@@ -19,56 +19,53 @@ $ composer require thecodingmachine/gotenberg-php-client
19
19
## Usage
20
20
21
21
``` php
22
- <?php
23
-
24
- namespace YourAwesomeNamespace;
25
-
26
22
use TheCodingMachine\Gotenberg\Client;
27
23
use TheCodingMachine\Gotenberg\ClientException;
28
24
use TheCodingMachine\Gotenberg\DocumentFactory;
29
25
use TheCodingMachine\Gotenberg\HTMLRequest;
30
26
use TheCodingMachine\Gotenberg\Request;
31
27
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.
32
36
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');
34
62
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.
72
69
}
73
70
```
74
71
0 commit comments