14
14
* @param Stream[] $pdfs
15
15
* @param array<string,string|bool|float|int|array<string>> $metadata
16
16
*/
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 {
18
18
$ pdfEngines = Gotenberg::pdfEngines ('' )->index (new DummyIndex ());
19
19
20
20
if ($ pdfa !== null ) {
@@ -29,12 +29,16 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
29
29
$ pdfEngines ->metadata ($ metadata );
30
30
}
31
31
32
+ if ($ flatten ) {
33
+ $ pdfEngines ->flattening ();
34
+ }
35
+
32
36
$ request = $ pdfEngines ->merge (...$ pdfs );
33
37
$ body = sanitize ($ request ->getBody ()->getContents ());
34
38
35
39
expect ($ request ->getUri ()->getPath ())->toBe ('/forms/pdfengines/merge ' );
36
40
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 ' ));
38
42
39
43
if (count ($ metadata ) > 0 ) {
40
44
$ json = json_encode ($ metadata );
@@ -45,6 +49,8 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
45
49
expect ($ body )->toContainFormValue ('metadata ' , $ json );
46
50
}
47
51
52
+ expect ($ body )->unless ($ flatten === false , fn ($ body ) => $ body ->toContainFormValue ('flatten ' , '1 ' ));
53
+
48
54
foreach ($ pdfs as $ pdf ) {
49
55
$ pdf ->getStream ()->rewind ();
50
56
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
66
72
'PDF/A-1a ' ,
67
73
true ,
68
74
[ 'Producer ' => 'Gotenberg ' ],
75
+ true ,
69
76
],
70
77
]);
71
78
72
79
it (
73
80
'creates a valid request for the "/forms/pdfengines/split" endpoint ' ,
74
81
/** @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
+ }
77
100
78
101
$ request = $ pdfEngines ->split ($ mode , ...$ pdfs );
79
102
$ body = sanitize ($ request ->getBody ()->getContents ());
@@ -82,10 +105,23 @@ function (array $pdfs, SplitMode $mode): void {
82
105
expect ($ body )->toContainFormValue ('splitMode ' , $ mode ->mode );
83
106
expect ($ body )->toContainFormValue ('splitSpan ' , $ mode ->span );
84
107
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 ' ));
85
121
86
122
foreach ($ pdfs as $ pdf ) {
87
123
$ 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 ' );
89
125
}
90
126
},
91
127
)->with ([
@@ -102,17 +138,28 @@ function (array $pdfs, SplitMode $mode): void {
102
138
Stream::string ('my_third.pdf ' , 'Third PDF content ' ),
103
139
],
104
140
SplitMode::pages ('1-2 ' , true ),
141
+ 'PDF/A-1a ' ,
142
+ true ,
143
+ [ 'Producer ' => 'Gotenberg ' ],
144
+ true ,
105
145
],
106
146
]);
107
147
108
148
it (
109
149
'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 );
112
158
$ body = sanitize ($ request ->getBody ()->getContents ());
113
159
114
160
expect ($ request ->getUri ()->getPath ())->toBe ('/forms/pdfengines/convert ' );
115
161
expect ($ body )->toContainFormValue ('pdfa ' , $ pdfa );
162
+ expect ($ body )->unless ($ pdfua === false , fn ($ body ) => $ body ->toContainFormValue ('pdfua ' , '1 ' ));
116
163
117
164
foreach ($ pdfs as $ pdf ) {
118
165
$ pdf ->getStream ()->rewind ();
@@ -122,15 +169,43 @@ function (string $pdfa, Stream ...$pdfs): void {
122
169
)->with ([
123
170
[
124
171
'PDF/A-1a ' ,
172
+ false ,
125
173
Stream::string ('my.pdf ' , 'PDF content ' ),
126
174
],
127
175
[
128
176
'PDF/A-1a ' ,
177
+ true ,
129
178
Stream::string ('my.pdf ' , 'PDF content ' ),
130
179
Stream::string ('my_second.pdf ' , 'Second PDF content ' ),
131
180
],
132
181
]);
133
182
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
+
134
209
it (
135
210
'creates a valid request for the "/forms/pdfengines/metadata/read" endpoint ' ,
136
211
/** @param Stream[] $pdfs */
0 commit comments