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
7987it (
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