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

Commit

Permalink
Merge pull request #10 from thecodingmachine/6.1.0
Browse files Browse the repository at this point in the history
6.1.0
  • Loading branch information
gulien authored Dec 10, 2019
2 parents 44e0369 + 887716f commit 1829b37
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 46 deletions.
81 changes: 39 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,53 @@ $ composer require thecodingmachine/gotenberg-php-client
## Usage

```php
<?php

namespace YourAwesomeNamespace;

use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\ClientException;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\HTMLRequest;
use TheCodingMachine\Gotenberg\Request;
use TheCodingMachine\Gotenberg\RequestException;
use GuzzleHttp\Psr7\LazyOpenStream;

# create the client.
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
# ... or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
$client = new Client('http://localhost:3000');

# prepare the files required for your conversion.

class YourAwesomeClass {
# from a path.
$index = DocumentFactory::makeFromPath('index.html', '/path/to/file');
# ... or from your own stream.
$stream = new LazyOpenStream('/path/to/file', 'r');
$index = DocumentFactory::makeFromStream('index.html', $stream);
// ... or from a string.
$index = DocumentFactory::makeFromString('index.html', '<html>Foo</html>');

$header = DocumentFactory::makeFromPath('header.html', '/path/to/file');
$footer = DocumentFactory::makeFromPath('footer.html', '/path/to/file');
$assets = [
DocumentFactory::makeFromPath('style.css', '/path/to/file'),
DocumentFactory::makeFromPath('img.png', '/path/to/file'),
];

try {
$request = new HTMLRequest($index);
$request->setHeader($header);
$request->setFooter($footer);
$request->setAssets($assets);
$request->setPaperSize(Request::A4);
$request->setMargins(Request::NO_MARGINS);

# store method allows you to... store the resulting PDF in a particular destination.
$client->store($request, 'path/you/want/the/pdf/to/be/stored.pdf');

public function yourAwesomeMethod()
{
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
# or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
$client = new Client('http://localhost:3000');

# HTML conversion example.
$index = DocumentFactory::makeFromPath('index.html', '/path/to/file');
$header = DocumentFactory::makeFromPath('header.html', '/path/to/file');
$footer = DocumentFactory::makeFromPath('footer.html', '/path/to/file');
$assets = [
DocumentFactory::makeFromPath('style.css', '/path/to/file'),
DocumentFactory::makeFromPath('img.png', '/path/to/file'),
];

try {
$request = new HTMLRequest($index);
$request->setHeader($header);
$request->setFooter($footer);
$request->setAssets($assets);
$request->setPaperSize(Request::A4);
$request->setMargins(Request::NO_MARGINS);

# store method allows you to... store the resulting PDF in a particular destination.
$client->store($request, 'path/you/want/the/pdf/to/be/stored.pdf');

# if you wish to redirect the response directly to the browser, you may also use:
$client->post($request);

} catch (RequestException $e) {
# this exception is thrown if given paper size or margins are not correct.
} catch (ClientException $e) {
# this exception is thrown by the client if the API has returned a code != 200.
} catch (\Exception $e) {
# some (random?) exception.
}
}
# if you wish to redirect the response directly to the browser, you may also use:
$client->post($request);
} catch (RequestException $e) {
# this exception is thrown if given paper size or margins are not correct.
} catch (ClientException $e) {
# this exception is thrown by the client if the API has returned a code != 200.
}
```

Expand Down
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ services:
image: thecodingmachine/php:7.3-v2-cli
container_name: php
environment:
- PHP_EXTENSION_XDEBUG=1
PHP_EXTENSION_XDEBUG: 1
restart: 'no'
volumes:
- ./:/usr/src/app:rw

gotenberg:
image: thecodingmachine/gotenberg:6.0.1
image: thecodingmachine/gotenberg:6
environment:
LOG_LEVEL: 'DEBUG'
DEFAULT_WAIT_TIMEOUT: '30.0'
container_name: gotenberg
restart: 'no'
12 changes: 12 additions & 0 deletions src/ChromeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract class ChromeRequest extends Request implements GotenbergRequestInterfac
private const MARGIN_LEFT = 'marginLeft';
private const MARGIN_RIGHT = 'marginRight';
private const LANDSCAPE = 'landscape';
private const PAGE_RANGES = 'pageRanges';
private const GOOGLE_CHROME_RPCC_BUFFER_SIZE = 'googleChromeRpccBufferSize';

/** @var float|null */
Expand Down Expand Up @@ -48,6 +49,9 @@ abstract class ChromeRequest extends Request implements GotenbergRequestInterfac
/** @var bool */
private $landscape;

/** @var string|null */
private $pageRanges;

/** @var int|null */
private $googleChromeRpccBufferSize;

Expand Down Expand Up @@ -78,6 +82,9 @@ public function getFormValues(): array
if ($this->marginRight !== null) {
$values[self::MARGIN_RIGHT] = $this->marginRight;
}
if ($this->pageRanges !== null) {
$values[self::PAGE_RANGES] = $this->pageRanges;
}
if ($this->googleChromeRpccBufferSize !== null) {
$values[self::GOOGLE_CHROME_RPCC_BUFFER_SIZE] = $this->googleChromeRpccBufferSize;
}
Expand Down Expand Up @@ -182,6 +189,11 @@ public function setLandscape(bool $landscape): void
$this->landscape = $landscape;
}

public function setPageRanges(string $pageRanges): void
{
$this->pageRanges = $pageRanges;
}

public function setGoogleChromeRpccBufferSize(?int $googleChromeRpccBufferSize): void
{
$this->googleChromeRpccBufferSize = $googleChromeRpccBufferSize;
Expand Down
12 changes: 10 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ public function post(GotenbergRequestInterface $request): ResponseInterface
* Sends the given documents to the API, stores the resulting PDF in the given destination.
*
* @throws ClientException
* @throws RequestException
* @throws Exception
* @throws FilesystemException
*/
public function store(GotenbergRequestInterface $request, string $destination): void
{
if ($request->hasWebhook()) {
throw new RequestException('Cannot use method store with a webhook.');
}
$response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
$fileStream = $response->getBody();
$fp = fopen($destination, 'w');
Expand Down Expand Up @@ -79,11 +83,15 @@ private function makeMultipartFormDataRequest(GotenbergRequestInterface $request
}
$body = new MultipartStream($multipartData);
$messageFactory = MessageFactoryDiscovery::find();

return $messageFactory
$message = $messageFactory
->createRequest('POST', $this->apiURL . $request->getPostURL())
->withHeader('Content-Type', 'multipart/form-data; boundary="' . $body->getBoundary() . '"')
->withBody($body);
foreach ($request->getCustomHTTPHeaders() as $key => $value) {
$message = $message->withHeader($key, $value);
}

return $message;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/GotenbergRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ interface GotenbergRequestInterface
{
public function getPostURL(): string;

/**
* @return array<string,string>
*/
public function getCustomHTTPHeaders(): array;

/**
* @return array<string,mixed>
*/
Expand All @@ -17,4 +22,6 @@ public function getFormValues(): array;
* @return array<string,Document>
*/
public function getFormFiles(): array;

public function hasWebhook(): bool;
}
1 change: 1 addition & 0 deletions src/HTMLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HTMLRequest extends ChromeRequest implements GotenbergRequestInterface

public function __construct(Document $index)
{
parent::__construct();
$this->index = $index;
$this->assets = [];
}
Expand Down
1 change: 1 addition & 0 deletions src/MergeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class MergeRequest extends Request implements GotenbergRequestInterface
*/
public function __construct(array $files)
{
parent::__construct();
$this->files = $files;
}

Expand Down
13 changes: 13 additions & 0 deletions src/OfficeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
final class OfficeRequest extends Request implements GotenbergRequestInterface
{
private const LANDSCAPE = 'landscape';
private const PAGE_RANGES = 'pageRanges';

/** @var Document[] */
private $files;

/** @var bool */
private $landscape;

/** @var string|null */
private $pageRanges;

/**
* @param Document[] $files
*/
public function __construct(array $files)
{
parent::__construct();
$this->files = $files;
}

Expand All @@ -33,6 +38,9 @@ public function getPostURL(): string
public function getFormValues(): array
{
$values = parent::getFormValues();
if ($this->pageRanges !== null) {
$values[self::PAGE_RANGES] = $this->pageRanges;
}
$values[self::LANDSCAPE] = $this->landscape;

return $values;
Expand All @@ -55,4 +63,9 @@ public function setLandscape(bool $landscape): void
{
$this->landscape = $landscape;
}

public function setPageRanges(string $pageRanges): void
{
$this->pageRanges = $pageRanges;
}
}
29 changes: 29 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ abstract class Request
private const WEBHOOK_URL = 'webhookURL';
private const WEBHOOK_URL_TIMEOUT = 'webhookURLTimeout';

private const WEBHOOK_URL_BASE_HTTP_HEADER_KEY = 'Gotenberg-Webhookurl-';

/** @var string|null */
private $resultFilename;

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

/** @var array<string,string> */
protected $customHTTPHeaders;

public function __construct()
{
$this->customHTTPHeaders = [];
}

/**
* @return array<string,mixed>
*/
Expand All @@ -57,6 +67,19 @@ public function getFormValues(): array
return $values;
}

public function hasWebhook(): bool
{
return ! empty($this->webhookURL);
}

/**
* @return array<string,string>
*/
public function getCustomHTTPHeaders(): array
{
return $this->customHTTPHeaders;
}

public function setResultFilename(?string $resultFilename): void
{
$this->resultFilename = $resultFilename;
Expand All @@ -76,4 +99,10 @@ public function setWebhookURLTimeout(?float $webhookURLTimeout): void
{
$this->webhookURLTimeout = $webhookURLTimeout;
}

public function addWebhookURLHTTPHeader(string $key, string $value): void
{
$key = self::WEBHOOK_URL_BASE_HTTP_HEADER_KEY . $key;
$this->customHTTPHeaders[$key] = $value;
}
}
9 changes: 9 additions & 0 deletions src/URLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ final class URLRequest extends ChromeRequest implements GotenbergRequestInterfac
{
private const REMOTE_URL = 'remoteURL';

private const REMOTE_URL_BASE_HTTP_HEADER_KEY = 'Gotenberg-Remoteurl-';

/** @var string */
private $URL;

public function __construct(string $URL)
{
parent::__construct();
$this->URL = $URL;
}

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

return $values;
}

public function addRemoteURLHTTPHeader(string $key, string $value): void
{
$key = self::REMOTE_URL_BASE_HTTP_HEADER_KEY . $key;
$this->customHTTPHeaders[$key] = $value;
}
}
Loading

0 comments on commit 1829b37

Please sign in to comment.