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
14
2
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.
25
4
26
5
## Install
27
6
@@ -37,31 +16,6 @@ Then the PHP client:
37
16
$ composer require thecodingmachine/gotenberg-php-client
38
17
```
39
18
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
-
65
19
## Usage
66
20
67
21
``` php
@@ -72,8 +26,9 @@ namespace YourAwesomeNamespace;
72
26
use TheCodingMachine\Gotenberg\Client;
73
27
use TheCodingMachine\Gotenberg\ClientException;
74
28
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;
77
32
78
33
class YourAwesomeClass {
79
34
@@ -83,39 +38,47 @@ class YourAwesomeClass {
83
38
# or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
84
39
$client = new Client('gotenberg:3000');
85
40
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
+ ];
89
49
90
- # now let's send those documents!
91
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
+
92
58
# store method allows you to... store the resulting PDF in a particular folder.
93
59
# 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');
98
61
99
62
# 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);
104
64
65
+ } catch (RequestException $e) {
66
+ # this exception is thrown is given paper size or margins are not correct.
105
67
} catch (ClientException $e) {
106
68
# this exception is thrown by the client if the API has returned a code != 200.
107
69
} catch (\Http\Client\Exception $e) {
108
70
# some PSR7 exception.
109
71
} catch (\Exception $e) {
110
72
# some (random?) exception.
111
73
}
112
- }
113
-
74
+ }
114
75
}
115
76
```
116
77
117
- Voilà! : smiley :
78
+ For more complete usages, head to the [ documentation ] ( https://thecodingmachine.github.io/gotenberg ) .
118
79
119
- ---
80
+ ## Badges
120
81
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 )
0 commit comments