Skip to content

Commit c325cc7

Browse files
committed
feat: add the encrypt feature and add missing flatten method in the Chromium module
1 parent 7df8a49 commit c325cc7

6 files changed

Lines changed: 173 additions & 2 deletions

File tree

src/Modules/ChromiumPdf.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,27 @@ public function metadata(array $metadata): self
215215
return $this;
216216
}
217217

218+
/**
219+
* Defines whether the resulting PDF should be flattened.
220+
*/
221+
public function flatten(): self
222+
{
223+
$this->formValue('flatten', true);
224+
225+
return $this;
226+
}
227+
228+
/**
229+
* Defines whether the resulting PDF should be encrypted.
230+
*/
231+
public function encrypt(string $userPassword, string $ownerPassword = ''): self
232+
{
233+
$this->formValue('userPassword', $userPassword);
234+
$this->formValue('ownerPassword', $ownerPassword);
235+
236+
return $this;
237+
}
238+
218239
/**
219240
* Converts a target URL to PDF.
220241
*

src/Modules/LibreOffice.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,17 @@ public function flatten(): self
358358
return $this;
359359
}
360360

361+
/**
362+
* Defines whether the resulting PDF should be encrypted.
363+
*/
364+
public function encrypt(string $userPassword, string $ownerPassword = ''): self
365+
{
366+
$this->formValue('userPassword', $userPassword);
367+
$this->formValue('ownerPassword', $ownerPassword);
368+
369+
return $this;
370+
}
371+
361372
/**
362373
* Converts the given document(s) to PDF(s). Gotenberg will return either
363374
* a unique PDF if you request a merge or a ZIP archive with the PDFs.

src/Modules/PdfEngines.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ public function flattening(): self
8181
return $this;
8282
}
8383

84+
/**
85+
* Defines whether the resulting PDF should be encrypted.
86+
* Prefer the encrypt method if you only want to encrypt one or more PDFs.
87+
*/
88+
public function encrypting(string $userPassword, string $ownerPassword = ''): self
89+
{
90+
$this->formValue('userPassword', $userPassword);
91+
$this->formValue('ownerPassword', $ownerPassword);
92+
93+
return $this;
94+
}
95+
8496
/**
8597
* Merges PDFs into a unique PDF.
8698
*
@@ -186,4 +198,22 @@ public function writeMetadata(array $metadata, Stream ...$pdfs): RequestInterfac
186198

187199
return $this->request();
188200
}
201+
202+
/**
203+
* Allows encrypting one or more PDF.
204+
*
205+
* @throws NativeFunctionErrored
206+
*/
207+
public function encrypt(string $userPassword, string $ownerPassword = '', Stream ...$pdfs): RequestInterface
208+
{
209+
$this->encrypting($userPassword, $ownerPassword);
210+
211+
foreach ($pdfs as $pdf) {
212+
$this->formFile($pdf->getFilename(), $pdf->getStream());
213+
}
214+
215+
$this->endpoint = '/forms/pdfengines/encrypt';
216+
217+
return $this->request();
218+
}
189219
}

tests/Modules/ChromiumPdfTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ function (
5353
string|null $pdfa = null,
5454
bool $pdfua = false,
5555
array $metadata = [],
56+
bool $flatten = false,
57+
string $userPassword = '',
58+
string $ownerPassword = '',
5659
array $assets = [],
5760
): void {
5861
$chromium = Gotenberg::chromium('')->pdf();
@@ -90,6 +93,9 @@ function (
9093
$pdfa,
9194
$pdfua,
9295
$metadata,
96+
$flatten,
97+
$userPassword,
98+
$ownerPassword,
9399
$assets,
94100
);
95101

@@ -133,6 +139,9 @@ function (
133139
$pdfa,
134140
$pdfua,
135141
$metadata,
142+
$flatten,
143+
$userPassword,
144+
$ownerPassword,
136145
$assets,
137146
);
138147
},
@@ -179,6 +188,9 @@ function (
179188
'PDF/A-1a',
180189
true,
181190
[ 'Producer' => 'Gotenberg' ],
191+
true,
192+
'my_user_password',
193+
'my_owner_password',
182194
[
183195
Stream::string('my.jpg', 'Image content'),
184196
],
@@ -229,6 +241,9 @@ function (
229241
string|null $pdfa = null,
230242
bool $pdfua = false,
231243
array $metadata = [],
244+
bool $flatten = false,
245+
string $userPassword = '',
246+
string $ownerPassword = '',
232247
array $assets = [],
233248
): void {
234249
$chromium = Gotenberg::chromium('')->pdf();
@@ -266,6 +281,9 @@ function (
266281
$pdfa,
267282
$pdfua,
268283
$metadata,
284+
$flatten,
285+
$userPassword,
286+
$ownerPassword,
269287
$assets,
270288
);
271289

@@ -311,6 +329,9 @@ function (
311329
$pdfa,
312330
$pdfua,
313331
$metadata,
332+
$flatten,
333+
$userPassword,
334+
$ownerPassword,
314335
$assets,
315336
);
316337
},
@@ -356,6 +377,9 @@ function (
356377
'PDF/A-1a',
357378
true,
358379
[ 'Producer' => 'Gotenberg' ],
380+
true,
381+
'my_user_password',
382+
'my_owner_password',
359383
[
360384
Stream::string('my.jpg', 'Image content'),
361385
],
@@ -408,6 +432,9 @@ function (
408432
string|null $pdfa = null,
409433
bool $pdfua = false,
410434
array $metadata = [],
435+
bool $flatten = false,
436+
string $userPassword = '',
437+
string $ownerPassword = '',
411438
array $assets = [],
412439
): void {
413440
$chromium = Gotenberg::chromium('')->pdf();
@@ -445,6 +472,9 @@ function (
445472
$pdfa,
446473
$pdfua,
447474
$metadata,
475+
$flatten,
476+
$userPassword,
477+
$ownerPassword,
448478
$assets,
449479
);
450480

@@ -495,6 +525,9 @@ function (
495525
$pdfa,
496526
$pdfua,
497527
$metadata,
528+
$flatten,
529+
$userPassword,
530+
$ownerPassword,
498531
$assets,
499532
);
500533
},
@@ -549,6 +582,9 @@ function (
549582
'PDF/A-1a',
550583
true,
551584
[ 'Producer' => 'Gotenberg' ],
585+
true,
586+
'my_user_password',
587+
'my_owner_password',
552588
[
553589
Stream::string('my.jpg', 'Image content'),
554590
],
@@ -597,6 +633,9 @@ function hydrateChromiumPdfFormData(
597633
string|null $pdfa = null,
598634
bool $pdfua = false,
599635
array $metadata = [],
636+
bool $flatten = false,
637+
string $userPassword = '',
638+
string $ownerPassword = '',
600639
array $assets = [],
601640
): ChromiumPdf {
602641
if ($singlePage) {
@@ -715,6 +754,14 @@ function hydrateChromiumPdfFormData(
715754
$chromium->metadata($metadata);
716755
}
717756

757+
if ($flatten) {
758+
$chromium->flatten();
759+
}
760+
761+
if ($userPassword !== '') {
762+
$chromium->encrypt($userPassword, $ownerPassword);
763+
}
764+
718765
if (count($assets) > 0) {
719766
$chromium->assets(...$assets);
720767
}
@@ -764,6 +811,9 @@ function expectChromiumPdfOptions(
764811
string|null $pdfa,
765812
bool $pdfua,
766813
array $metadata,
814+
bool $flatten,
815+
string $userPassword,
816+
string $ownerPassword,
767817
array $assets,
768818
): void {
769819
expect($body)->unless($singlePage === false, fn ($body) => $body->toContainFormValue('singlePage', '1'));
@@ -865,6 +915,10 @@ function expectChromiumPdfOptions(
865915
expect($body)->toContainFormValue('metadata', $json);
866916
}
867917

918+
expect($body)->unless($flatten === false, fn ($body) => $body->toContainFormValue('flatten', '1'));
919+
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('userPassword', $userPassword));
920+
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('ownerPassword', $ownerPassword));
921+
868922
if (count($assets) <= 0) {
869923
return;
870924
}

tests/Modules/LibreOfficeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ function (
4545
array $metadata = [],
4646
bool $merge = false,
4747
bool $flatten = false,
48+
string $userPassword = '',
49+
string $ownerPassword = '',
4850
): void {
4951
$libreOffice = Gotenberg::libreOffice('');
5052

@@ -166,6 +168,10 @@ function (
166168
$libreOffice->flatten();
167169
}
168170

171+
if ($userPassword !== '') {
172+
$libreOffice->encrypt($userPassword, $ownerPassword);
173+
}
174+
169175
$request = $libreOffice->convert(...$files);
170176
$body = sanitize($request->getBody()->getContents());
171177

@@ -204,6 +210,8 @@ function (
204210
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));
205211
expect($body)->unless($merge === false, fn ($body) => $body->toContainFormValue('merge', '1'));
206212
expect($body)->unless($flatten === false, fn ($body) => $body->toContainFormValue('flatten', '1'));
213+
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('userPassword', $userPassword));
214+
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('ownerPassword', $ownerPassword));
207215

208216
if (count($metadata) > 0) {
209217
$json = json_encode($metadata);
@@ -261,5 +269,7 @@ function (
261269
[ 'Producer' => 'Gotenberg' ],
262270
true,
263271
true,
272+
'my_user_password',
273+
'my_owner_password',
264274
],
265275
]);

tests/Modules/PdfEnginesTest.php

Lines changed: 47 additions & 2 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 = [], bool $flatten = false): void {
17+
function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $metadata = [], bool $flatten = false, string $userPassword = '', string $ownerPassword = ''): void {
1818
$pdfEngines = Gotenberg::pdfEngines('')->index(new DummyIndex());
1919

2020
if ($pdfa !== null) {
@@ -33,6 +33,10 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
3333
$pdfEngines->flattening();
3434
}
3535

36+
if ($userPassword !== '') {
37+
$pdfEngines->encrypting($userPassword, $ownerPassword);
38+
}
39+
3640
$request = $pdfEngines->merge(...$pdfs);
3741
$body = sanitize($request->getBody()->getContents());
3842

@@ -50,6 +54,8 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
5054
}
5155

5256
expect($body)->unless($flatten === false, fn ($body) => $body->toContainFormValue('flatten', '1'));
57+
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('userPassword', $userPassword));
58+
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('ownerPassword', $ownerPassword));
5359

5460
foreach ($pdfs as $pdf) {
5561
$pdf->getStream()->rewind();
@@ -73,13 +79,15 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
7379
true,
7480
[ 'Producer' => 'Gotenberg' ],
7581
true,
82+
'my_user_password',
83+
'my_owner_password',
7684
],
7785
]);
7886

7987
it(
8088
'creates a valid request for the "/forms/pdfengines/split" endpoint',
8189
/** @param Stream[] $pdfs */
82-
function (array $pdfs, SplitMode $mode, string|null $pdfa = null, bool $pdfua = false, array $metadata = [], bool $flatten = false): void {
90+
function (array $pdfs, SplitMode $mode, string|null $pdfa = null, bool $pdfua = false, array $metadata = [], bool $flatten = false, string $userPassword = '', string $ownerPassword = ''): void {
8391
$pdfEngines = Gotenberg::pdfEngines('');
8492

8593
if ($pdfa !== null) {
@@ -98,6 +106,10 @@ function (array $pdfs, SplitMode $mode, string|null $pdfa = null, bool $pdfua =
98106
$pdfEngines->flattening();
99107
}
100108

109+
if ($userPassword !== '') {
110+
$pdfEngines->encrypting($userPassword, $ownerPassword);
111+
}
112+
101113
$request = $pdfEngines->split($mode, ...$pdfs);
102114
$body = sanitize($request->getBody()->getContents());
103115

@@ -118,6 +130,8 @@ function (array $pdfs, SplitMode $mode, string|null $pdfa = null, bool $pdfua =
118130
}
119131

120132
expect($body)->unless($flatten === false, fn ($body) => $body->toContainFormValue('flatten', '1'));
133+
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('userPassword', $userPassword));
134+
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('ownerPassword', $ownerPassword));
121135

122136
foreach ($pdfs as $pdf) {
123137
$pdf->getStream()->rewind();
@@ -142,6 +156,8 @@ function (array $pdfs, SplitMode $mode, string|null $pdfa = null, bool $pdfua =
142156
true,
143157
[ 'Producer' => 'Gotenberg' ],
144158
true,
159+
'my_user_password',
160+
'my_owner_password',
145161
],
146162
]);
147163

@@ -266,3 +282,32 @@ function (array $metadata, array $pdfs): void {
266282
],
267283
],
268284
]);
285+
286+
it(
287+
'creates a valid request for the "/forms/pdfengines/encrypt" endpoint',
288+
/** @param Stream[] $pdfs */
289+
function (string $userPassword, string $ownerPassword, array $pdfs): void {
290+
$pdfEngines = Gotenberg::pdfEngines('');
291+
292+
$request = $pdfEngines->encrypt($userPassword, $ownerPassword, ...$pdfs);
293+
$body = sanitize($request->getBody()->getContents());
294+
295+
expect($request->getUri()->getPath())->toBe('/forms/pdfengines/encrypt');
296+
expect($body)->toContainFormValue('userPassword', $userPassword);
297+
expect($body)->toContainFormValue('ownerPassword', $ownerPassword);
298+
299+
foreach ($pdfs as $pdf) {
300+
$pdf->getStream()->rewind();
301+
expect($body)->toContainFormFile($pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
302+
}
303+
},
304+
)->with([
305+
[
306+
'my_user_password',
307+
'my_owner_password',
308+
[
309+
Stream::string('my.pdf', 'PDF content'),
310+
Stream::string('my_second.pdf', 'Second PDF content'),
311+
],
312+
],
313+
]);

0 commit comments

Comments
 (0)