Skip to content

Commit f466c8d

Browse files
committed
feat(pdfengines/libreoffice): add flatten feature
1 parent 2cf43a9 commit f466c8d

File tree

5 files changed

+136
-12
lines changed

5 files changed

+136
-12
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@
6363
"phpstan analyse -l max src tests"
6464
],
6565
"lint:fix": "phpcbf",
66-
"tests": "XDEBUG_MODE=coverage pest --coverage --coverage-html coverage_html --coverage-clover coverage.xml"
66+
"tests": "XDEBUG_MODE=coverage pest --coverage --coverage-html coverage_html --coverage-clover coverage.xml",
67+
"all": [
68+
"@composer run lint:fix",
69+
"@composer run lint",
70+
"@composer run tests"
71+
]
6772
},
6873
"config": {
6974
"sort-packages": true,

src/Modules/LibreOffice.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ public function split(SplitMode $mode): self
337337
return $this;
338338
}
339339

340+
/**
341+
* Defines whether the resulting PDF should be flattened.
342+
*/
343+
public function flatten(): self
344+
{
345+
$this->formValue('flatten', true);
346+
347+
return $this;
348+
}
349+
340350
/**
341351
* Converts the given document(s) to PDF(s). Gotenberg will return either
342352
* a unique PDF if you request a merge or a ZIP archive with the PDFs.

src/Modules/PdfEngines.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ public function metadata(array $metadata): self
7070
return $this;
7171
}
7272

73+
/**
74+
* Defines whether the resulting PDF should be flattened.
75+
* Prefer the flatten method if you only want to flatten one or more PDFs.
76+
*/
77+
public function flattening(): self
78+
{
79+
$this->formValue('flatten', true);
80+
81+
return $this;
82+
}
83+
7384
/**
7485
* Merges PDFs into a unique PDF.
7586
*
@@ -89,25 +100,41 @@ public function merge(Stream ...$pdfs): RequestInterface
89100
}
90101

91102
/**
92-
* Splits PDFs.
103+
* Splits PDF(s).
104+
* Gotenberg will return the PDF or a ZIP archive with the PDFs.
93105
*/
94106
public function split(SplitMode $mode, Stream ...$pdfs): RequestInterface
95107
{
96108
$this->formValue('splitMode', $mode->mode);
97109
$this->formValue('splitSpan', $mode->span);
98110
$this->formValue('splitUnify', $mode->unify ?: '0');
99111

100-
$index = $this->index ?? new HrtimeIndex();
101-
102112
foreach ($pdfs as $pdf) {
103-
$this->formFile($index->create() . '_' . $pdf->getFilename(), $pdf->getStream());
113+
$this->formFile($pdf->getFilename(), $pdf->getStream());
104114
}
105115

106116
$this->endpoint = '/forms/pdfengines/split';
107117

108118
return $this->request();
109119
}
110120

121+
/**
122+
* Flatten PDF(s).
123+
* Gotenberg will return the PDF or a ZIP archive with the PDFs.
124+
*/
125+
public function flatten(Stream ...$pdfs): RequestInterface
126+
{
127+
$this->formValue('flatten', true);
128+
129+
foreach ($pdfs as $pdf) {
130+
$this->formFile($pdf->getFilename(), $pdf->getStream());
131+
}
132+
133+
$this->endpoint = '/forms/pdfengines/flatten';
134+
135+
return $this->request();
136+
}
137+
111138
/**
112139
* Converts PDF(s) to a specific PDF/A format.
113140
* Gotenberg will return the PDF or a ZIP archive with the PDFs.

tests/Modules/LibreOfficeTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function (
4343
bool $pdfua = false,
4444
array $metadata = [],
4545
bool $merge = false,
46+
bool $flatten = false,
4647
): void {
4748
$libreOffice = Gotenberg::libreOffice('');
4849

@@ -156,6 +157,10 @@ function (
156157
->merge();
157158
}
158159

160+
if ($flatten) {
161+
$libreOffice->flatten();
162+
}
163+
159164
$request = $libreOffice->convert(...$files);
160165
$body = sanitize($request->getBody()->getContents());
161166

@@ -192,6 +197,7 @@ function (
192197
expect($body)->unless($pdfa === null, fn ($body) => $body->toContainFormValue('pdfa', $pdfa));
193198
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));
194199
expect($body)->unless($merge === false, fn ($body) => $body->toContainFormValue('merge', '1'));
200+
expect($body)->unless($flatten === false, fn ($body) => $body->toContainFormValue('flatten', '1'));
195201

196202
if (count($metadata) > 0) {
197203
$json = json_encode($metadata);
@@ -247,5 +253,6 @@ function (
247253
true,
248254
[ 'Producer' => 'Gotenberg' ],
249255
true,
256+
true,
250257
],
251258
]);

tests/Modules/PdfEnginesTest.php

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @param Stream[] $pdfs
1515
* @param array<string,string|bool|float|int|array<string>> $metadata
1616
*/
17-
function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $metadata = []): void {
17+
function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $metadata = [], bool $flatten = false): void {
1818
$pdfEngines = Gotenberg::pdfEngines('')->index(new DummyIndex());
1919

2020
if ($pdfa !== null) {
@@ -29,12 +29,16 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
2929
$pdfEngines->metadata($metadata);
3030
}
3131

32+
if ($flatten) {
33+
$pdfEngines->flattening();
34+
}
35+
3236
$request = $pdfEngines->merge(...$pdfs);
3337
$body = sanitize($request->getBody()->getContents());
3438

3539
expect($request->getUri()->getPath())->toBe('/forms/pdfengines/merge');
3640
expect($body)->unless($pdfa === null, fn ($body) => $body->toContainFormValue('pdfa', $pdfa));
37-
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfa', $pdfa));
41+
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));
3842

3943
if (count($metadata) > 0) {
4044
$json = json_encode($metadata);
@@ -45,6 +49,8 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
4549
expect($body)->toContainFormValue('metadata', $json);
4650
}
4751

52+
expect($body)->unless($flatten === false, fn ($body) => $body->toContainFormValue('flatten', '1'));
53+
4854
foreach ($pdfs as $pdf) {
4955
$pdf->getStream()->rewind();
5056
expect($body)->toContainFormFile('foo_' . $pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
@@ -66,14 +72,31 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
6672
'PDF/A-1a',
6773
true,
6874
[ 'Producer' => 'Gotenberg' ],
75+
true,
6976
],
7077
]);
7178

7279
it(
7380
'creates a valid request for the "/forms/pdfengines/split" endpoint',
7481
/** @param Stream[] $pdfs */
75-
function (array $pdfs, SplitMode $mode): void {
76-
$pdfEngines = Gotenberg::pdfEngines('')->index(new DummyIndex());
82+
function (array $pdfs, SplitMode $mode, string|null $pdfa = null, bool $pdfua = false, array $metadata = [], bool $flatten = false): void {
83+
$pdfEngines = Gotenberg::pdfEngines('');
84+
85+
if ($pdfa !== null) {
86+
$pdfEngines->pdfa($pdfa);
87+
}
88+
89+
if ($pdfua) {
90+
$pdfEngines->pdfua();
91+
}
92+
93+
if (count($metadata) > 0) {
94+
$pdfEngines->metadata($metadata);
95+
}
96+
97+
if ($flatten) {
98+
$pdfEngines->flattening();
99+
}
77100

78101
$request = $pdfEngines->split($mode, ...$pdfs);
79102
$body = sanitize($request->getBody()->getContents());
@@ -82,10 +105,23 @@ function (array $pdfs, SplitMode $mode): void {
82105
expect($body)->toContainFormValue('splitMode', $mode->mode);
83106
expect($body)->toContainFormValue('splitSpan', $mode->span);
84107
expect($body)->toContainFormValue('splitUnify', $mode->unify ? '1' : '0');
108+
expect($body)->unless($pdfa === null, fn ($body) => $body->toContainFormValue('pdfa', $pdfa));
109+
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));
110+
111+
if (count($metadata) > 0) {
112+
$json = json_encode($metadata);
113+
if ($json === false) {
114+
throw NativeFunctionErrored::createFromLastPhpError();
115+
}
116+
117+
expect($body)->toContainFormValue('metadata', $json);
118+
}
119+
120+
expect($body)->unless($flatten === false, fn ($body) => $body->toContainFormValue('flatten', '1'));
85121

86122
foreach ($pdfs as $pdf) {
87123
$pdf->getStream()->rewind();
88-
expect($body)->toContainFormFile('foo_' . $pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
124+
expect($body)->toContainFormFile($pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
89125
}
90126
},
91127
)->with([
@@ -102,17 +138,28 @@ function (array $pdfs, SplitMode $mode): void {
102138
Stream::string('my_third.pdf', 'Third PDF content'),
103139
],
104140
SplitMode::pages('1-2', true),
141+
'PDF/A-1a',
142+
true,
143+
[ 'Producer' => 'Gotenberg' ],
144+
true,
105145
],
106146
]);
107147

108148
it(
109149
'creates a valid request for the "/forms/pdfengines/convert" endpoint',
110-
function (string $pdfa, Stream ...$pdfs): void {
111-
$request = Gotenberg::pdfEngines('')->convert($pdfa, ...$pdfs);
150+
function (string $pdfa, bool $pdfua, Stream ...$pdfs): void {
151+
$pdfEngines = Gotenberg::pdfEngines('');
152+
153+
if ($pdfua) {
154+
$pdfEngines->pdfua();
155+
}
156+
157+
$request = $pdfEngines->convert($pdfa, ...$pdfs);
112158
$body = sanitize($request->getBody()->getContents());
113159

114160
expect($request->getUri()->getPath())->toBe('/forms/pdfengines/convert');
115161
expect($body)->toContainFormValue('pdfa', $pdfa);
162+
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));
116163

117164
foreach ($pdfs as $pdf) {
118165
$pdf->getStream()->rewind();
@@ -122,15 +169,43 @@ function (string $pdfa, Stream ...$pdfs): void {
122169
)->with([
123170
[
124171
'PDF/A-1a',
172+
false,
125173
Stream::string('my.pdf', 'PDF content'),
126174
],
127175
[
128176
'PDF/A-1a',
177+
true,
129178
Stream::string('my.pdf', 'PDF content'),
130179
Stream::string('my_second.pdf', 'Second PDF content'),
131180
],
132181
]);
133182

183+
it(
184+
'creates a valid request for the "/forms/pdfengines/flatten" endpoint',
185+
/** @param Stream[] $pdfs */
186+
function (array $pdfs): void {
187+
$pdfEngines = Gotenberg::pdfEngines('');
188+
189+
$request = $pdfEngines->flatten(...$pdfs);
190+
$body = sanitize($request->getBody()->getContents());
191+
192+
expect($request->getUri()->getPath())->toBe('/forms/pdfengines/flatten');
193+
expect($body)->toContainFormValue('flatten', '1');
194+
195+
foreach ($pdfs as $pdf) {
196+
$pdf->getStream()->rewind();
197+
expect($body)->toContainFormFile($pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
198+
}
199+
},
200+
)->with([
201+
[
202+
[
203+
Stream::string('my.pdf', 'PDF content'),
204+
Stream::string('my_second.pdf', 'Second PDF content'),
205+
],
206+
],
207+
]);
208+
134209
it(
135210
'creates a valid request for the "/forms/pdfengines/metadata/read" endpoint',
136211
/** @param Stream[] $pdfs */

0 commit comments

Comments
 (0)