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

Commit 0218eef

Browse files
author
Julien Neuhart
committed
updating for Gotenberg v3
1 parent 136b0e7 commit 0218eef

39 files changed

+814
-169
lines changed

README.md

+30-67
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
1-
<h3 align="center">Gotenberg PHP Client</h3>
2-
<p align="center">A PHP client for the Gotenberg API</p>
3-
<p align="center">
4-
<a href="https://travis-ci.org/thecodingmachine/gotenberg-php-client">
5-
<img src="https://travis-ci.org/thecodingmachine/gotenberg-php-client.svg?branch=master" alt="Travis CI">
6-
</a>
7-
<a href="https://scrutinizer-ci.com/g/thecodingmachine/gotenberg-php-client/?branch=master">
8-
<img src="https://scrutinizer-ci.com/g/thecodingmachine/gotenberg-php-client/badges/quality-score.png?b=master" alt="Scrutinizer">
9-
</a>
10-
<a href="https://codecov.io/gh/thecodingmachine/gotenberg-php-client/branch/master">
11-
<img src="https://codecov.io/gh/thecodingmachine/gotenberg-php-client/branch/master/graph/badge.svg" alt="Codecov">
12-
</a>
13-
</p>
1+
# Gotenberg PHP client
142

15-
---
16-
17-
[Gotenberg](https://github.com/thecodingmachine/gotenberg) is a stateless API for converting Markdown files, HTML files and Office documents to PDF.
18-
This package helps you to interact with this API using PHP.
19-
20-
# Menu
21-
22-
* [Install](#install)
23-
* [Docker](#docker)
24-
* [Usage](#usage)
3+
A simple PHP client for interacting with a Gotenberg API.
254

265
## Install
276

@@ -37,31 +16,6 @@ Then the PHP client:
3716
$ composer require thecodingmachine/gotenberg-php-client
3817
```
3918

40-
## Docker
41-
42-
As the [Gotenberg](https://github.com/thecodingmachine/gotenberg) API is provided within a Docker image, you'll have to add it
43-
to your Docker Compose stack:
44-
45-
```yaml
46-
version: '3'
47-
48-
services:
49-
50-
# your others services
51-
52-
gotenberg:
53-
image: thecodingmachine/gotenberg:2.0.0
54-
```
55-
56-
You may now start your stack using:
57-
58-
```bash
59-
$ docker-compose up --scale gotenberg=your_number_of_instances
60-
```
61-
62-
When requesting the Gotenberg service with your client, Docker will automatically redirect a request to a Gotenberg container
63-
according to the round-robin strategy.
64-
6519
## Usage
6620

6721
```php
@@ -72,8 +26,9 @@ namespace YourAwesomeNamespace;
7226
use TheCodingMachine\Gotenberg\Client;
7327
use TheCodingMachine\Gotenberg\ClientException;
7428
use TheCodingMachine\Gotenberg\DocumentFactory;
75-
76-
use GuzzleHttp\Psr7\LazyOpenStream;
29+
use TheCodingMachine\Gotenberg\HTMLRequest;
30+
use TheCodingMachine\Gotenberg\Request;
31+
use TheCodingMachine\Gotenberg\RequestException;
7732

7833
class YourAwesomeClass {
7934

@@ -83,39 +38,47 @@ class YourAwesomeClass {
8338
# or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
8439
$client = new Client('gotenberg:3000');
8540

86-
# let's instantiate some documents you wish to convert.
87-
$yourOfficeDocument = DocumentFactory::makeFromPath('file.docx', '/path/to/file');
88-
$yourHTMLDocument = DocumentFactory::makeFromStream('file.html', new LazyOpenStream('path/to/file', 'r'));
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+
];
8949

90-
# now let's send those documents!
9150
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+
9258
# store method allows you to... store the resulting PDF in a particular folder.
9359
# this method also returns the resulting PDF path.
94-
$filePath = $client->store([
95-
$yourOfficeDocument,
96-
$yourHTMLDocument
97-
], 'path/to/folder/you/want/the/pdf/to/be/store');
60+
$filePath = $client->store($request, 'path/to/folder/you/want/the/pdf/to/be/store');
9861

9962
# if you wish to redirect the response directly to the browser, you may also use:
100-
$response = $client->forward([
101-
$yourOfficeDocument,
102-
$yourHTMLDocument
103-
]);
63+
$response = $client->post($request);
10464

65+
} catch (RequestException $e) {
66+
# this exception is thrown is given paper size or margins are not correct.
10567
} catch (ClientException $e) {
10668
# this exception is thrown by the client if the API has returned a code != 200.
10769
} catch (\Http\Client\Exception $e) {
10870
# some PSR7 exception.
10971
} catch (\Exception $e) {
11072
# some (random?) exception.
11173
}
112-
}
113-
74+
}
11475
}
11576
```
11677

117-
Voilà! :smiley:
78+
For more complete usages, head to the [documentation](https://thecodingmachine.github.io/gotenberg).
11879

119-
---
80+
## Badges
12081

121-
Would you like to update this documentation ? Feel free to open an [issue](../../issues).
82+
[![Travis CI](https://travis-ci.org/thecodingmachine/gotenberg-php-client.svg?branch=master)](https://travis-ci.org/thecodingmachine/gotenberg-php-client)
83+
[![Scrutinizer](https://scrutinizer-ci.com/g/thecodingmachine/gotenberg-php-client/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/thecodingmachine/gotenberg-php-client/?branch=master)
84+
[![Codecov](https://codecov.io/gh/thecodingmachine/gotenberg-php-client/branch/master/graph/badge.svg)](https://codecov.io/gh/thecodingmachine/gotenberg-php-client/branch/master)

composer.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
"php-http/httplug": "^1.0",
2929
"php-http/discovery": "^1.0",
3030
"guzzlehttp/psr7": "^1.4.2",
31-
"php-http/message": "^1.0"
31+
"php-http/message": "^1.0",
32+
"thecodingmachine/safe": "^0.1.8"
3233
},
3334
"require-dev" : {
3435
"phpunit/phpunit": "^7",
3536
"squizlabs/php_codesniffer": "^3.2",
36-
"phpstan/phpstan": "^0.9.2",
37-
"thecodingmachine/phpstan-strict-rules": "^0.9.0",
37+
"phpstan/phpstan": "^0.10.5",
38+
"thecodingmachine/phpstan-safe-rule": "^0.1.0",
39+
"thecodingmachine/phpstan-strict-rules": "^0.10.7",
3840
"php-http/mock-client": "^1.0",
3941
"php-http/guzzle6-adapter": "^1.1"
4042
},
@@ -56,7 +58,7 @@
5658
},
5759
"extra": {
5860
"branch-alias": {
59-
"dev-master": "2.0.x-dev"
61+
"dev-master": "3.0.x-dev"
6062
}
6163
}
6264
}

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: '3'
33
services:
44

55
php:
6-
image: thecodingmachine/php:7.1-v1-cli
6+
image: thecodingmachine/php:7.2-v2-cli
77
container_name: php
88
environment:
99
- PHP_EXTENSION_XDEBUG=1
@@ -12,6 +12,6 @@ services:
1212
- ./:/usr/src/app:rw
1313

1414
gotenberg:
15-
image: thecodingmachine/gotenberg:2.0.0
15+
image: thecodingmachine/gotenberg:3
1616
container_name: gotenberg
1717
restart: 'no'

phpstan.neon

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#parameters:
2-
# ignoreErrors:
3-
# - "#Instantiated class WeakRef not found.#"
41
includes:
5-
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
2+
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
3+
- vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon

phpunit.xml.dist

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="false">
10+
stopOnFailure="false">
1211

1312
<testsuites>
1413
<testsuite name="Test Suite">

src/Client.php

+29-21
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
use Http\Discovery\MessageFactoryDiscovery;
99
use Psr\Http\Message\RequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
11+
use Safe\Exceptions\FilesystemException;
12+
use function Safe\mkdir;
13+
use function Safe\fopen;
14+
use function Safe\fwrite;
15+
use function Safe\fclose;
1116

12-
class Client
17+
final class Client
1318
{
1419
/** @var HttpClient */
1520
private $client;
@@ -31,67 +36,70 @@ public function __construct(string $apiURL, HttpClient $client = null)
3136
/**
3237
* Sends the given documents to the API and returns the response.
3338
*
34-
* @param Document[] $documents
39+
* @param GotenbergRequestInterface $request
3540
* @return ResponseInterface
3641
* @throws ClientException
3742
* @throws \Exception
38-
* @throws \Http\Client\Exception
43+
* @throws FilesystemException
3944
*/
40-
public function forward(array $documents): ResponseInterface
45+
public function post(GotenbergRequestInterface $request): ResponseInterface
4146
{
42-
return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($documents)));
47+
return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
4348
}
4449

4550
/**
4651
* Sends the given documents to the API, stores the resulting PDF in the given storing path
4752
* and returns the resulting PDF path.
4853
*
49-
* @param Document[] $documents
54+
* @param GotenbergRequestInterface $request
5055
* @param string $storingPath
5156
* @return string
5257
* @throws ClientException
5358
* @throws \Exception
54-
* @throws \Http\Client\Exception
59+
* @throws FilesystemException
5560
*/
56-
public function store(array $documents, string $storingPath): string
61+
public function store(GotenbergRequestInterface $request, string $storingPath): string
5762
{
58-
$response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($documents)));
59-
63+
$response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
6064
if (!is_dir($storingPath)) {
6165
mkdir($storingPath);
6266
}
63-
6467
$filePath = $storingPath . '/' . uniqid() . '.pdf';
6568
$fileStream = $response->getBody();
66-
6769
$fp = fopen($filePath, 'w');
6870
fwrite($fp, $fileStream);
6971
fclose($fp);
70-
7172
return $filePath;
7273
}
7374

7475
/**
75-
* @param Document[] $documents
76+
* @param GotenbergRequestInterface $request
7677
* @return RequestInterface
7778
*/
78-
private function makeMultipartFormDataRequest(array $documents): RequestInterface
79+
private function makeMultipartFormDataRequest(GotenbergRequestInterface $request): RequestInterface
7980
{
8081
$multipartData = [];
81-
82-
foreach ($documents as $document) {
82+
foreach ($request->getFormValues() as $fieldName => $fieldValue) {
83+
$multipartData[] = [
84+
'name' => $fieldName,
85+
'contents' => $fieldValue,
86+
];
87+
}
88+
/**
89+
* @var string $filename
90+
* @var Document $document
91+
*/
92+
foreach ($request->getFormFiles() as $filename => $document) {
8393
$multipartData[] = [
8494
'name' => 'files',
85-
'filename' => $document->getFileName(),
95+
'filename' => $filename,
8696
'contents' => $document->getFileStream()
8797
];
8898
}
89-
9099
$body = new MultipartStream($multipartData);
91100
$messageFactory = MessageFactoryDiscovery::find();
92-
93101
return $messageFactory
94-
->createRequest('POST', $this->apiURL)
102+
->createRequest('POST', $this->apiURL . $request->getPostURL())
95103
->withHeader('Content-Type', 'multipart/form-data; boundary="' . $body->getBoundary() . '"')
96104
->withBody($body);
97105
}

src/ClientException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace TheCodingMachine\Gotenberg;
44

5-
class ClientException extends \Exception
5+
final class ClientException extends \Exception
66
{
77

88
}

src/Document.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Psr\Http\Message\StreamInterface;
66

7-
class Document
7+
final class Document
88
{
99
/** @var string */
1010
private $fileName;

src/DocumentFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use GuzzleHttp\Psr7\LazyOpenStream;
66
use Psr\Http\Message\StreamInterface;
77

8-
class DocumentFactory
8+
final class DocumentFactory
99
{
1010
/**
1111
* @param string $fileName

src/GotenbergRequestInterface.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace TheCodingMachine\Gotenberg;
4+
5+
interface GotenbergRequestInterface
6+
{
7+
/**
8+
* @return string
9+
*/
10+
public function getPostURL(): string;
11+
12+
/**
13+
* @return array<string,mixed>
14+
*/
15+
public function getFormValues(): array;
16+
17+
/**
18+
* @return array<string,Document>
19+
*/
20+
public function getFormFiles(): array;
21+
}

0 commit comments

Comments
 (0)