collaboraDomain = $parameters->get('wopi')['server']; } public function __invoke(StoredObject $storedObject): Response { if (!$this->security->getUser() instanceof User) { throw new AccessDeniedHttpException('User must be authenticated'); } $content = $this->storedObjectManager->read($storedObject); $query = []; if (null !== $request = $this->requestStack->getCurrentRequest()) { $query['lang'] = $request->getLocale(); } try { $url = sprintf('%s/cool/convert-to/pdf', $this->collaboraDomain); $form = new FormDataPart([ 'data' => new DataPart($content, $storedObject->getUuid()->toString(), $storedObject->getType()), ]); $response = $this->httpClient->request('POST', $url, [ 'headers' => $form->getPreparedHeaders()->toArray(), 'query' => $query, 'body' => $form->bodyToString(), 'timeout' => 10, ]); return new Response($response->getContent(), Response::HTTP_OK, [ 'Content-Type' => 'application/pdf', ]); } catch (ClientExceptionInterface|TransportExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface $exception) { return $this->onConversionFailed($url, $exception->getResponse()); } } private function onConversionFailed(string $url, ResponseInterface $response): JsonResponse { $this->logger->error(self::LOG_PREFIX . ' could not convert document', [ 'response_status' => $response->getStatusCode(), 'message' => $response->getContent(false), 'server' => $this->collaboraDomain, 'url' => $url, ]); return new JsonResponse(['message' => 'conversion failed : ' . $response->getContent(false)], Response::HTTP_SERVICE_UNAVAILABLE); } }