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

Commit 70cab57

Browse files
author
Julien Neuhart
committed
updating for Gotenberg 4
1 parent c556fe9 commit 70cab57

13 files changed

+296
-196
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ class YourAwesomeClass {
5555
$request->setPaperSize(Request::A4);
5656
$request->setMargins(Request::NO_MARGINS);
5757

58-
# store method allows you to... store the resulting PDF in a particular folder.
59-
# this method also returns the resulting PDF path.
60-
$filePath = $client->store($request, 'path/to/folder/you/want/the/pdf/to/be/store');
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');
6160

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

6564
} catch (RequestException $e) {
6665
# this exception is thrown if given paper size or margins are not correct.

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ services:
1212
- ./:/usr/src/app:rw
1313

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

src/ChromeRequest.php

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
namespace TheCodingMachine\Gotenberg;
4+
5+
abstract class ChromeRequest extends Request implements GotenbergRequestInterface
6+
{
7+
/** @var Document|null */
8+
protected $header;
9+
10+
/** @var Document|null */
11+
protected $footer;
12+
13+
/** @var float|null */
14+
protected $paperWidth;
15+
16+
/** @var float|null */
17+
protected $paperHeight;
18+
19+
/** @var float|null */
20+
protected $marginTop;
21+
22+
/** @var float|null */
23+
protected $marginBottom;
24+
25+
/** @var float|null */
26+
protected $marginLeft;
27+
28+
/** @var float|null */
29+
protected $marginRight;
30+
31+
/** @var bool */
32+
protected $landscape;
33+
34+
/**
35+
* @return array<string,mixed>
36+
*/
37+
public function getFormValues(): array
38+
{
39+
$values = [];
40+
if (!empty($this->webhookURL)) {
41+
$values[self::WEBHOOK_URL] = $this->webhookURL;
42+
}
43+
if (!is_null($this->paperWidth)) {
44+
$values[self::PAPER_WIDTH] = $this->paperWidth;
45+
}
46+
if (!is_null($this->paperHeight)) {
47+
$values[self::PAPER_HEIGHT] = $this->paperHeight;
48+
}
49+
if (!is_null($this->marginTop)) {
50+
$values[self::MARGIN_TOP] = $this->marginTop;
51+
}
52+
if (!is_null($this->marginBottom)) {
53+
$values[self::MARGIN_BOTTOM] = $this->marginBottom;
54+
}
55+
if (!is_null($this->marginLeft)) {
56+
$values[self::MARGIN_LEFT] = $this->marginLeft;
57+
}
58+
if (!is_null($this->marginRight)) {
59+
$values[self::MARGIN_RIGHT] = $this->marginRight;
60+
}
61+
$values[self::LANDSCAPE] = $this->landscape;
62+
return $values;
63+
}
64+
65+
/**
66+
* @return array<string,Document>
67+
*/
68+
public function getFormFiles(): array
69+
{
70+
$files = [];
71+
if (!empty($this->header)) {
72+
$files['header.html'] = $this->header;
73+
}
74+
if (!empty($this->footer)) {
75+
$files['footer.html'] = $this->footer;
76+
}
77+
return $files;
78+
}
79+
80+
/**
81+
* @param float[] $paperSize
82+
* @throws RequestException
83+
*/
84+
public function setPaperSize(array $paperSize): void
85+
{
86+
if (count($paperSize) !== 2) {
87+
throw new RequestException('Wrong paper size.');
88+
}
89+
$this->paperWidth = $paperSize[0];
90+
$this->paperHeight = $paperSize[1];
91+
}
92+
93+
/**
94+
* @param float[] $margins
95+
* @throws RequestException
96+
*/
97+
public function setMargins(array $margins): void
98+
{
99+
if (count($margins) !== 4) {
100+
throw new RequestException('Wrong margins.');
101+
}
102+
$this->marginTop = $margins[0];
103+
$this->marginBottom = $margins[1];
104+
$this->marginLeft = $margins[2];
105+
$this->marginRight = $margins[3];
106+
}
107+
108+
/**
109+
* @param Document|null $header
110+
*/
111+
public function setHeader(?Document $header): void
112+
{
113+
$this->header = $header;
114+
}
115+
116+
/**
117+
* @param Document|null $footer
118+
*/
119+
public function setFooter(?Document $footer): void
120+
{
121+
$this->footer = $footer;
122+
}
123+
124+
/**
125+
* @param float|null $paperWidth
126+
*/
127+
public function setPaperWidth(?float $paperWidth): void
128+
{
129+
$this->paperWidth = $paperWidth;
130+
}
131+
132+
/**
133+
* @param float|null $paperHeight
134+
*/
135+
public function setPaperHeight(?float $paperHeight): void
136+
{
137+
$this->paperHeight = $paperHeight;
138+
}
139+
140+
/**
141+
* @param float|null $marginTop
142+
*/
143+
public function setMarginTop(?float $marginTop): void
144+
{
145+
$this->marginTop = $marginTop;
146+
}
147+
148+
/**
149+
* @param float|null $marginBottom
150+
*/
151+
public function setMarginBottom(?float $marginBottom): void
152+
{
153+
$this->marginBottom = $marginBottom;
154+
}
155+
156+
/**
157+
* @param float|null $marginLeft
158+
*/
159+
public function setMarginLeft(?float $marginLeft): void
160+
{
161+
$this->marginLeft = $marginLeft;
162+
}
163+
164+
/**
165+
* @param float|null $marginRight
166+
*/
167+
public function setMarginRight(?float $marginRight): void
168+
{
169+
$this->marginRight = $marginRight;
170+
}
171+
172+
/**
173+
* @param bool $landscape
174+
*/
175+
public function setLandscape(bool $landscape): void
176+
{
177+
$this->landscape = $landscape;
178+
}
179+
}

src/Client.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,28 @@ public function __construct(string $apiURL, HttpClient $client = null)
4040
* @return ResponseInterface
4141
* @throws ClientException
4242
* @throws \Exception
43-
* @throws FilesystemException
4443
*/
4544
public function post(GotenbergRequestInterface $request): ResponseInterface
4645
{
4746
return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
4847
}
4948

5049
/**
51-
* Sends the given documents to the API, stores the resulting PDF in the given storing path
52-
* and returns the resulting PDF path.
50+
* Sends the given documents to the API, stores the resulting PDF in the given destination.
5351
*
5452
* @param GotenbergRequestInterface $request
55-
* @param string $storingPath
56-
* @return string
53+
* @param string $destination
5754
* @throws ClientException
5855
* @throws \Exception
5956
* @throws FilesystemException
6057
*/
61-
public function store(GotenbergRequestInterface $request, string $storingPath): string
58+
public function store(GotenbergRequestInterface $request, string $destination): void
6259
{
6360
$response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
64-
if (!is_dir($storingPath)) {
65-
mkdir($storingPath);
66-
}
67-
$filePath = $storingPath . '/' . uniqid() . '.pdf';
6861
$fileStream = $response->getBody();
69-
$fp = fopen($filePath, 'w');
62+
$fp = fopen($destination, 'w');
7063
fwrite($fp, $fileStream);
7164
fclose($fp);
72-
return $filePath;
7365
}
7466

7567
/**

src/DocumentFactory.php

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public static function makeFromString(string $fileName, string $string): Documen
4141
{
4242
$fileStream = fopen('php://memory', 'rb+');
4343
fwrite($fileStream, $string);
44-
4544
return new Document($fileName, stream_for($fileStream));
4645
}
4746
}

0 commit comments

Comments
 (0)