@@ -67,20 +67,23 @@ use mikehaertl\pdftk\Pdf;
67
67
68
68
// Fill form with data array
69
69
$pdf = new Pdf('/full/path/to/form.pdf');
70
- $pdf->fillForm([
70
+ $result = $ pdf->fillForm([
71
71
'name'=>'ÄÜÖ äüö мирано čárka',
72
72
'nested.name' => 'valX',
73
73
])
74
74
->needAppearances()
75
75
->saveAs('filled.pdf');
76
76
77
+ // Always check for errors
78
+ if ($result === false) {
79
+ $error = $pdf->getError();
80
+ }
81
+
77
82
// Fill form from FDF
78
83
$pdf = new Pdf('form.pdf');
79
- $pdf->fillForm('data.xfdf')
84
+ $result = $ pdf->fillForm('data.xfdf')
80
85
->saveAs('filled.pdf');
81
-
82
- // Check for errors
83
- if (!$pdf->saveAs('my.pdf')) {
86
+ if ($result === false) {
84
87
$error = $pdf->getError();
85
88
}
86
89
```
@@ -114,22 +117,28 @@ use mikehaertl\pdftk\Pdf;
114
117
115
118
// Extract pages 1-5 and 7,4,9 into a new file
116
119
$pdf = new Pdf('/path/to/my.pdf');
117
- $pdf->cat(1, 5)
120
+ $result = $ pdf->cat(1, 5)
118
121
->cat([7, 4, 9])
119
122
->saveAs('/path/to/new.pdf');
123
+ if ($result === false) {
124
+ $error = $pdf->getError();
125
+ }
120
126
121
127
// Combine pages from several files, demonstrating several ways how to add files
122
128
$pdf = new Pdf([
123
129
'A' => '/path/file1.pdf', // A is alias for file1.pdf
124
130
'B' => ['/path/file2.pdf','pass**word'], // B is alias for file2.pdf
125
131
]);
126
132
$pdf->addFile('/path/file3.pdf','C','**secret**pw'); // C is alias file3.pdf
127
- $pdf->cat(1, 5, 'A') // pages 1-5 from A
133
+ $result = $ pdf->cat(1, 5, 'A') // pages 1-5 from A
128
134
->cat(3, null, 'B') // page 3 from B
129
135
->cat(7, 'end', 'B', null, 'east') // pages 7-end from B, rotated East
130
136
->cat('end',3,'A','even') // even pages 3-end in reverse order from A
131
137
->cat([2,3,7], 'C') // pages 2,3 and 7 from C
132
138
->saveAs('/path/new.pdf');
139
+ if ($result === false) {
140
+ $error = $pdf->getError();
141
+ }
133
142
```
134
143
135
144
#### Shuffle
@@ -146,9 +155,12 @@ $pdf = new Pdf([
146
155
]);
147
156
148
157
// new.pdf will have pages A1, B3, A2, B4, A3, B5, ...
149
- $pdf->shuffle(1, 5, 'A') // pages 1-5 from A
158
+ $result = $ pdf->shuffle(1, 5, 'A') // pages 1-5 from A
150
159
->shuffle(3, 8, 'B') // pages 3-8 from B
151
160
->saveAs('/path/new.pdf');
161
+ if ($result === false) {
162
+ $error = $pdf->getError();
163
+ }
152
164
```
153
165
154
166
#### Burst
@@ -159,7 +171,10 @@ Split a PDF file into one file per page.
159
171
use mikehaertl\pdftk\Pdf;
160
172
161
173
$pdf = new Pdf('/path/my.pdf');
162
- $pdf->burst('/path/page_%d.pdf'); // Supply a printf() pattern
174
+ $result = $pdf->burst('/path/page_%d.pdf'); // Supply a printf() pattern
175
+ if ($result === false) {
176
+ $error = $pdf->getError();
177
+ }
163
178
```
164
179
165
180
#### Add background PDF
@@ -171,13 +186,19 @@ use mikehaertl\pdftk\Pdf;
171
186
172
187
// Set background from another PDF (first page repeated)
173
188
$pdf = new Pdf('/path/my.pdf');
174
- $pdf->background('/path/back.pdf')
189
+ $result = $ pdf->background('/path/back.pdf')
175
190
->saveAs('/path/watermarked.pdf');
191
+ if ($result === false) {
192
+ $error = $pdf->getError();
193
+ }
176
194
177
195
// Set background from another PDF (one page each)
178
196
$pdf = new Pdf('/path/my.pdf');
179
- $pdf->multiBackground('/path/back_pages.pdf')
197
+ $result = $ pdf->multiBackground('/path/back_pages.pdf')
180
198
->saveAs('/path/watermarked.pdf');
199
+ if ($result === false) {
200
+ $error = $pdf->getError();
201
+ }
181
202
```
182
203
183
204
#### Add overlay PDF
@@ -189,13 +210,19 @@ use mikehaertl\pdftk\Pdf;
189
210
190
211
// Stamp with another PDF (first page repeated)
191
212
$pdf = new Pdf('/path/my.pdf');
192
- $pdf->stamp('/path/overlay.pdf')
213
+ $result = $ pdf->stamp('/path/overlay.pdf')
193
214
->saveAs('/path/stamped.pdf');
215
+ if ($result === false) {
216
+ $error = $pdf->getError();
217
+ }
194
218
195
219
// Stamp with another PDF (one page each)
196
220
$pdf = new Pdf('/path/my.pdf');
197
- $pdf->multiStamp('/path/overlay_pages.pdf')
221
+ $result = $ pdf->multiStamp('/path/overlay_pages.pdf')
198
222
->saveAs('/path/stamped.pdf');
223
+ if ($result === false) {
224
+ $error = $pdf->getError();
225
+ }
199
226
```
200
227
201
228
#### Unpack Files
@@ -206,7 +233,10 @@ Copy file attachments from a PDF to the given directory.
206
233
use mikehaertl\pdftk\Pdf;
207
234
208
235
$pdf = new Pdf('/path/my.pdf');
209
- $pdf->unpackFiles('/path/to/dir');
236
+ $result = $pdf->unpackFiles('/path/to/dir');
237
+ if ($result === false) {
238
+ $error = $pdf->getError();
239
+ }
210
240
```
211
241
212
242
#### Generate FDF
@@ -218,7 +248,10 @@ use mikehaertl\pdftk\Pdf;
218
248
219
249
// Create FDF from PDF
220
250
$pdf = new Pdf('/path/form.pdf');
221
- $pdf->generateFdfFile('/path/data.fdf');
251
+ $result = $pdf->generateFdfFile('/path/data.fdf');
252
+ if ($result === false) {
253
+ $error = $pdf->getError();
254
+ }
222
255
```
223
256
224
257
#### Get PDF data
@@ -231,10 +264,16 @@ use mikehaertl\pdftk\Pdf;
231
264
// Get data
232
265
$pdf = new Pdf('/path/my.pdf');
233
266
$data = $pdf->getData();
267
+ if ($data === false) {
268
+ $error = $pdf->getError();
269
+ }
234
270
235
271
// Get form data fields
236
272
$pdf = new Pdf('/path/my.pdf');
237
273
$data = $pdf->getDataFields();
274
+ if ($data === false) {
275
+ $error = $pdf->getError();
276
+ }
238
277
239
278
// Get data as string
240
279
echo $data;
@@ -262,9 +301,12 @@ $pdf->cat(1, 5)
262
301
263
302
// We now use the above PDF as source file for a new PDF
264
303
$pdf2 = new Pdf($pdf);
265
- $pdf2->fillForm(['name' => 'ÄÜÖ äüö мирано čárka'])
304
+ $result = $ pdf2->fillForm(['name' => 'ÄÜÖ äüö мирано čárka'])
266
305
->needAppearances()
267
306
->saveAs('/path/filled.pdf');
307
+ if ($result === false) {
308
+ $error = $pdf->getError();
309
+ }
268
310
```
269
311
270
312
### Options
@@ -276,7 +318,7 @@ use mikehaertl\pdftk\Pdf;
276
318
277
319
$pdf = new Pdf('/path/my.pdf');
278
320
279
- $pdf->allow('AllFeatures') // Change permissions
321
+ $result = $ pdf->allow('AllFeatures') // Change permissions
280
322
->flatten() // Merge form data into document (doesn't work well with UTF-8!)
281
323
->compress($value) // Compress/Uncompress
282
324
->keepId('first') // Keep first/last Id of combined files
@@ -287,18 +329,27 @@ $pdf->allow('AllFeatures') // Change permissions
287
329
->setUserPassword($pw) // Set user password
288
330
->passwordEncryption(128) // Set password encryption strength
289
331
->saveAs('new.pdf');
332
+ if ($result === false) {
333
+ $error = $pdf->getError();
334
+ }
290
335
291
336
// Example: Fill PDF form and merge form data into PDF
292
337
// Fill form with data array
293
- $pdf = new Pdf('/path/form.pdf');
338
+ $result = $ pdf = new Pdf('/path/form.pdf');
294
339
$pdf->fillForm(['name' => 'My Name'])
295
340
->flatten()
296
341
->saveAs('/path/filled.pdf');
342
+ if ($result === false) {
343
+ $error = $pdf->getError();
344
+ }
297
345
298
346
// Example: Remove password from a PDF
299
347
$pdf = new Pdf;
300
- $pdf->addFile('/path/my.pdf', null, 'some**password')
348
+ $result = $ pdf->addFile('/path/my.pdf', null, 'some**password')
301
349
->saveAs('/path/new.pdf');
350
+ if ($result === false) {
351
+ $error = $pdf->getError();
352
+ }
302
353
```
303
354
304
355
### Shell Command
@@ -327,8 +378,11 @@ file but only need the binary PDF content:
327
378
use mikehaertl\pdftk\Pdf;
328
379
329
380
$pdf = new Pdf('/path/my.pdf');
330
- $pdf->fillForm(['name' => 'My Name'])
381
+ $result = $ pdf->fillForm(['name' => 'My Name'])
331
382
->execute();
383
+ if ($result === false) {
384
+ $error = $pdf->getError();
385
+ }
332
386
$content = file_get_contents( (string) $pdf->getTmpFile() );
333
387
```
334
388
0 commit comments