Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit 1829b37

Browse files
author
Julien Neuhart
authored
Merge pull request #10 from thecodingmachine/6.1.0
6.1.0
2 parents 44e0369 + 887716f commit 1829b37

12 files changed

+190
-46
lines changed

README.md

+39-42
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,53 @@ $ composer require thecodingmachine/gotenberg-php-client
1919
## Usage
2020

2121
```php
22-
<?php
23-
24-
namespace YourAwesomeNamespace;
25-
2622
use TheCodingMachine\Gotenberg\Client;
2723
use TheCodingMachine\Gotenberg\ClientException;
2824
use TheCodingMachine\Gotenberg\DocumentFactory;
2925
use TheCodingMachine\Gotenberg\HTMLRequest;
3026
use TheCodingMachine\Gotenberg\Request;
3127
use TheCodingMachine\Gotenberg\RequestException;
28+
use GuzzleHttp\Psr7\LazyOpenStream;
29+
30+
# create the client.
31+
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
32+
# ... or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
33+
$client = new Client('http://localhost:3000');
34+
35+
# prepare the files required for your conversion.
3236

33-
class YourAwesomeClass {
37+
# from a path.
38+
$index = DocumentFactory::makeFromPath('index.html', '/path/to/file');
39+
# ... or from your own stream.
40+
$stream = new LazyOpenStream('/path/to/file', 'r');
41+
$index = DocumentFactory::makeFromStream('index.html', $stream);
42+
// ... or from a string.
43+
$index = DocumentFactory::makeFromString('index.html', '<html>Foo</html>');
44+
45+
$header = DocumentFactory::makeFromPath('header.html', '/path/to/file');
46+
$footer = DocumentFactory::makeFromPath('footer.html', '/path/to/file');
47+
$assets = [
48+
DocumentFactory::makeFromPath('style.css', '/path/to/file'),
49+
DocumentFactory::makeFromPath('img.png', '/path/to/file'),
50+
];
51+
52+
try {
53+
$request = new HTMLRequest($index);
54+
$request->setHeader($header);
55+
$request->setFooter($footer);
56+
$request->setAssets($assets);
57+
$request->setPaperSize(Request::A4);
58+
$request->setMargins(Request::NO_MARGINS);
59+
60+
# store method allows you to... store the resulting PDF in a particular destination.
61+
$client->store($request, 'path/you/want/the/pdf/to/be/stored.pdf');
3462

35-
public function yourAwesomeMethod()
36-
{
37-
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
38-
# or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
39-
$client = new Client('http://localhost:3000');
40-
41-
# HTML conversion example.
42-
$index = DocumentFactory::makeFromPath('index.html', '/path/to/file');
43-
$header = DocumentFactory::makeFromPath('header.html', '/path/to/file');
44-
$footer = DocumentFactory::makeFromPath('footer.html', '/path/to/file');
45-
$assets = [
46-
DocumentFactory::makeFromPath('style.css', '/path/to/file'),
47-
DocumentFactory::makeFromPath('img.png', '/path/to/file'),
48-
];
49-
50-
try {
51-
$request = new HTMLRequest($index);
52-
$request->setHeader($header);
53-
$request->setFooter($footer);
54-
$request->setAssets($assets);
55-
$request->setPaperSize(Request::A4);
56-
$request->setMargins(Request::NO_MARGINS);
57-
58-
# store method allows you to... store the resulting PDF in a particular destination.
59-
$client->store($request, 'path/you/want/the/pdf/to/be/stored.pdf');
60-
61-
# if you wish to redirect the response directly to the browser, you may also use:
62-
$client->post($request);
63-
64-
} catch (RequestException $e) {
65-
# this exception is thrown if given paper size or margins are not correct.
66-
} catch (ClientException $e) {
67-
# this exception is thrown by the client if the API has returned a code != 200.
68-
} catch (\Exception $e) {
69-
# some (random?) exception.
70-
}
71-
}
63+
# if you wish to redirect the response directly to the browser, you may also use:
64+
$client->post($request);
65+
} catch (RequestException $e) {
66+
# this exception is thrown if given paper size or margins are not correct.
67+
} catch (ClientException $e) {
68+
# this exception is thrown by the client if the API has returned a code != 200.
7269
}
7370
```
7471

docker-compose.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ services:
66
image: thecodingmachine/php:7.3-v2-cli
77
container_name: php
88
environment:
9-
- PHP_EXTENSION_XDEBUG=1
9+
PHP_EXTENSION_XDEBUG: 1
1010
restart: 'no'
1111
volumes:
1212
- ./:/usr/src/app:rw
1313

1414
gotenberg:
15-
image: thecodingmachine/gotenberg:6.0.1
15+
image: thecodingmachine/gotenberg:6
16+
environment:
17+
LOG_LEVEL: 'DEBUG'
18+
DEFAULT_WAIT_TIMEOUT: '30.0'
1619
container_name: gotenberg
1720
restart: 'no'

src/ChromeRequest.php

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ abstract class ChromeRequest extends Request implements GotenbergRequestInterfac
1616
private const MARGIN_LEFT = 'marginLeft';
1717
private const MARGIN_RIGHT = 'marginRight';
1818
private const LANDSCAPE = 'landscape';
19+
private const PAGE_RANGES = 'pageRanges';
1920
private const GOOGLE_CHROME_RPCC_BUFFER_SIZE = 'googleChromeRpccBufferSize';
2021

2122
/** @var float|null */
@@ -48,6 +49,9 @@ abstract class ChromeRequest extends Request implements GotenbergRequestInterfac
4849
/** @var bool */
4950
private $landscape;
5051

52+
/** @var string|null */
53+
private $pageRanges;
54+
5155
/** @var int|null */
5256
private $googleChromeRpccBufferSize;
5357

@@ -78,6 +82,9 @@ public function getFormValues(): array
7882
if ($this->marginRight !== null) {
7983
$values[self::MARGIN_RIGHT] = $this->marginRight;
8084
}
85+
if ($this->pageRanges !== null) {
86+
$values[self::PAGE_RANGES] = $this->pageRanges;
87+
}
8188
if ($this->googleChromeRpccBufferSize !== null) {
8289
$values[self::GOOGLE_CHROME_RPCC_BUFFER_SIZE] = $this->googleChromeRpccBufferSize;
8390
}
@@ -182,6 +189,11 @@ public function setLandscape(bool $landscape): void
182189
$this->landscape = $landscape;
183190
}
184191

192+
public function setPageRanges(string $pageRanges): void
193+
{
194+
$this->pageRanges = $pageRanges;
195+
}
196+
185197
public function setGoogleChromeRpccBufferSize(?int $googleChromeRpccBufferSize): void
186198
{
187199
$this->googleChromeRpccBufferSize = $googleChromeRpccBufferSize;

src/Client.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ public function post(GotenbergRequestInterface $request): ResponseInterface
4545
* Sends the given documents to the API, stores the resulting PDF in the given destination.
4646
*
4747
* @throws ClientException
48+
* @throws RequestException
4849
* @throws Exception
4950
* @throws FilesystemException
5051
*/
5152
public function store(GotenbergRequestInterface $request, string $destination): void
5253
{
54+
if ($request->hasWebhook()) {
55+
throw new RequestException('Cannot use method store with a webhook.');
56+
}
5357
$response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
5458
$fileStream = $response->getBody();
5559
$fp = fopen($destination, 'w');
@@ -79,11 +83,15 @@ private function makeMultipartFormDataRequest(GotenbergRequestInterface $request
7983
}
8084
$body = new MultipartStream($multipartData);
8185
$messageFactory = MessageFactoryDiscovery::find();
82-
83-
return $messageFactory
86+
$message = $messageFactory
8487
->createRequest('POST', $this->apiURL . $request->getPostURL())
8588
->withHeader('Content-Type', 'multipart/form-data; boundary="' . $body->getBoundary() . '"')
8689
->withBody($body);
90+
foreach ($request->getCustomHTTPHeaders() as $key => $value) {
91+
$message = $message->withHeader($key, $value);
92+
}
93+
94+
return $message;
8795
}
8896

8997
/**

src/GotenbergRequestInterface.php

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ interface GotenbergRequestInterface
88
{
99
public function getPostURL(): string;
1010

11+
/**
12+
* @return array<string,string>
13+
*/
14+
public function getCustomHTTPHeaders(): array;
15+
1116
/**
1217
* @return array<string,mixed>
1318
*/
@@ -17,4 +22,6 @@ public function getFormValues(): array;
1722
* @return array<string,Document>
1823
*/
1924
public function getFormFiles(): array;
25+
26+
public function hasWebhook(): bool;
2027
}

src/HTMLRequest.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class HTMLRequest extends ChromeRequest implements GotenbergRequestInterface
1414

1515
public function __construct(Document $index)
1616
{
17+
parent::__construct();
1718
$this->index = $index;
1819
$this->assets = [];
1920
}

src/MergeRequest.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class MergeRequest extends Request implements GotenbergRequestInterface
1414
*/
1515
public function __construct(array $files)
1616
{
17+
parent::__construct();
1718
$this->files = $files;
1819
}
1920

src/OfficeRequest.php

+13
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
final class OfficeRequest extends Request implements GotenbergRequestInterface
88
{
99
private const LANDSCAPE = 'landscape';
10+
private const PAGE_RANGES = 'pageRanges';
1011

1112
/** @var Document[] */
1213
private $files;
1314

1415
/** @var bool */
1516
private $landscape;
1617

18+
/** @var string|null */
19+
private $pageRanges;
20+
1721
/**
1822
* @param Document[] $files
1923
*/
2024
public function __construct(array $files)
2125
{
26+
parent::__construct();
2227
$this->files = $files;
2328
}
2429

@@ -33,6 +38,9 @@ public function getPostURL(): string
3338
public function getFormValues(): array
3439
{
3540
$values = parent::getFormValues();
41+
if ($this->pageRanges !== null) {
42+
$values[self::PAGE_RANGES] = $this->pageRanges;
43+
}
3644
$values[self::LANDSCAPE] = $this->landscape;
3745

3846
return $values;
@@ -55,4 +63,9 @@ public function setLandscape(bool $landscape): void
5563
{
5664
$this->landscape = $landscape;
5765
}
66+
67+
public function setPageRanges(string $pageRanges): void
68+
{
69+
$this->pageRanges = $pageRanges;
70+
}
5871
}

src/Request.php

+29
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ abstract class Request
2323
private const WEBHOOK_URL = 'webhookURL';
2424
private const WEBHOOK_URL_TIMEOUT = 'webhookURLTimeout';
2525

26+
private const WEBHOOK_URL_BASE_HTTP_HEADER_KEY = 'Gotenberg-Webhookurl-';
27+
2628
/** @var string|null */
2729
private $resultFilename;
2830

@@ -35,6 +37,14 @@ abstract class Request
3537
/** @var float|null */
3638
private $webhookURLTimeout;
3739

40+
/** @var array<string,string> */
41+
protected $customHTTPHeaders;
42+
43+
public function __construct()
44+
{
45+
$this->customHTTPHeaders = [];
46+
}
47+
3848
/**
3949
* @return array<string,mixed>
4050
*/
@@ -57,6 +67,19 @@ public function getFormValues(): array
5767
return $values;
5868
}
5969

70+
public function hasWebhook(): bool
71+
{
72+
return ! empty($this->webhookURL);
73+
}
74+
75+
/**
76+
* @return array<string,string>
77+
*/
78+
public function getCustomHTTPHeaders(): array
79+
{
80+
return $this->customHTTPHeaders;
81+
}
82+
6083
public function setResultFilename(?string $resultFilename): void
6184
{
6285
$this->resultFilename = $resultFilename;
@@ -76,4 +99,10 @@ public function setWebhookURLTimeout(?float $webhookURLTimeout): void
7699
{
77100
$this->webhookURLTimeout = $webhookURLTimeout;
78101
}
102+
103+
public function addWebhookURLHTTPHeader(string $key, string $value): void
104+
{
105+
$key = self::WEBHOOK_URL_BASE_HTTP_HEADER_KEY . $key;
106+
$this->customHTTPHeaders[$key] = $value;
107+
}
79108
}

src/URLRequest.php

+9
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ final class URLRequest extends ChromeRequest implements GotenbergRequestInterfac
88
{
99
private const REMOTE_URL = 'remoteURL';
1010

11+
private const REMOTE_URL_BASE_HTTP_HEADER_KEY = 'Gotenberg-Remoteurl-';
12+
1113
/** @var string */
1214
private $URL;
1315

1416
public function __construct(string $URL)
1517
{
18+
parent::__construct();
1619
$this->URL = $URL;
1720
}
1821

@@ -31,4 +34,10 @@ public function getFormValues(): array
3134

3235
return $values;
3336
}
37+
38+
public function addRemoteURLHTTPHeader(string $key, string $value): void
39+
{
40+
$key = self::REMOTE_URL_BASE_HTTP_HEADER_KEY . $key;
41+
$this->customHTTPHeaders[$key] = $value;
42+
}
3443
}

0 commit comments

Comments
 (0)