diff --git a/src/Client.php b/src/Client.php index 69e1472..99006f5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,6 +6,7 @@ use Exception; use GuzzleHttp\Psr7\MultipartStream; +use GuzzleHttp\Psr7\Response; use Http\Client\HttpClient; use Http\Discovery\HttpClientDiscovery; use Http\Discovery\MessageFactoryDiscovery; @@ -41,6 +42,21 @@ public function post(GotenbergRequestInterface $request): ResponseInterface return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request))); } + /** + * Sends the given documents to the API and returns the response diractly to the cleint. + * + * @throws ClientException + * @throws Exception + */ + public function stream(GotenbergRequestInterface $request): ResponseInterface + { + return new Response(200, [ + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'inline; filename=stream.pdf', + 'Cache-Control' => 'max-age=0', + ], $this->transformResponse($request)->getBody()); + } + /** * Sends the given documents to the API, stores the resulting PDF in the given destination. * @@ -106,4 +122,13 @@ private function handleResponse(ResponseInterface $response): ResponseInterface throw new ClientException($response->getBody()->getContents(), $response->getStatusCode()); } } + + /** + * @throws \Http\Client\Exception + * @throws ClientException + */ + private function transformResponse(GotenbergRequestInterface $request): ResponseInterface + { + return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request))); + } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 380bb06..39ca158 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -7,6 +7,7 @@ use HTTP\Client\Exception; use PHPUnit\Framework\TestCase; use Safe\Exceptions\FilesystemException; +use function method_exists; final class ClientTest extends TestCase { @@ -263,4 +264,45 @@ public function testRemoteURLHTTPHeader(): void $response = $client->post($request); $this->assertEquals(200, $response->getStatusCode()); } + + /** + * @throws RequestException + * @throws ClientException + */ + public function testStream(): void + { + $client = new Client(self::API_URL, new \Http\Adapter\Guzzle6\Client()); + // case 1: HTML. + $request = $this->createHTMLRequest(); + $request->setPageRanges('1-1'); + $this->assertCaseFor($request, $client); + + // case 2: URL. + $request = $this->createURLRequest(); + $request->setPageRanges('1-1'); + $this->assertCaseFor($request, $client); + // case 3: markdown. + $request = $this->createMarkdownRequest(); + $request->setPageRanges('1-1'); + $this->assertCaseFor($request, $client); + + // case 4: office. + $request = $this->createOfficeRequest(); + $request->setPageRanges('1-1'); + $this->assertCaseFor($request, $client); + } + + private function assertCaseFor($request, $client): void + { + if (method_exists($request, 'setWaitTimeout')) { + $request->setWaitTimeout(30.0); + } + + $response = $client->stream($request); + $this->assertEquals($response->getHeaderLine('Content-Type'), 'application/pdf'); + $this->assertEquals($response->getHeaderLine('Content-Disposition'), 'inline; filename=stream.pdf'); + $this->assertEquals($response->getHeaderLine('Cache-Control'), 'max-age=0'); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertNotEmpty($response->getBody()); + } }