Skip to content

Commit f1d29d3

Browse files
committed
Add error handling to all examples
1 parent d3eb534 commit f1d29d3

File tree

1 file changed

+74
-20
lines changed

1 file changed

+74
-20
lines changed

README.md

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,23 @@ use mikehaertl\pdftk\Pdf;
6767

6868
// Fill form with data array
6969
$pdf = new Pdf('/full/path/to/form.pdf');
70-
$pdf->fillForm([
70+
$result = $pdf->fillForm([
7171
'name'=>'ÄÜÖ äüö мирано čárka',
7272
'nested.name' => 'valX',
7373
])
7474
->needAppearances()
7575
->saveAs('filled.pdf');
7676

77+
// Always check for errors
78+
if ($result === false) {
79+
$error = $pdf->getError();
80+
}
81+
7782
// Fill form from FDF
7883
$pdf = new Pdf('form.pdf');
79-
$pdf->fillForm('data.xfdf')
84+
$result = $pdf->fillForm('data.xfdf')
8085
->saveAs('filled.pdf');
81-
82-
// Check for errors
83-
if (!$pdf->saveAs('my.pdf')) {
86+
if ($result === false) {
8487
$error = $pdf->getError();
8588
}
8689
```
@@ -114,22 +117,28 @@ use mikehaertl\pdftk\Pdf;
114117

115118
// Extract pages 1-5 and 7,4,9 into a new file
116119
$pdf = new Pdf('/path/to/my.pdf');
117-
$pdf->cat(1, 5)
120+
$result = $pdf->cat(1, 5)
118121
->cat([7, 4, 9])
119122
->saveAs('/path/to/new.pdf');
123+
if ($result === false) {
124+
$error = $pdf->getError();
125+
}
120126

121127
// Combine pages from several files, demonstrating several ways how to add files
122128
$pdf = new Pdf([
123129
'A' => '/path/file1.pdf', // A is alias for file1.pdf
124130
'B' => ['/path/file2.pdf','pass**word'], // B is alias for file2.pdf
125131
]);
126132
$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
128134
->cat(3, null, 'B') // page 3 from B
129135
->cat(7, 'end', 'B', null, 'east') // pages 7-end from B, rotated East
130136
->cat('end',3,'A','even') // even pages 3-end in reverse order from A
131137
->cat([2,3,7], 'C') // pages 2,3 and 7 from C
132138
->saveAs('/path/new.pdf');
139+
if ($result === false) {
140+
$error = $pdf->getError();
141+
}
133142
```
134143

135144
#### Shuffle
@@ -146,9 +155,12 @@ $pdf = new Pdf([
146155
]);
147156

148157
// 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
150159
->shuffle(3, 8, 'B') // pages 3-8 from B
151160
->saveAs('/path/new.pdf');
161+
if ($result === false) {
162+
$error = $pdf->getError();
163+
}
152164
```
153165

154166
#### Burst
@@ -159,7 +171,10 @@ Split a PDF file into one file per page.
159171
use mikehaertl\pdftk\Pdf;
160172

161173
$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+
}
163178
```
164179

165180
#### Add background PDF
@@ -171,13 +186,19 @@ use mikehaertl\pdftk\Pdf;
171186

172187
// Set background from another PDF (first page repeated)
173188
$pdf = new Pdf('/path/my.pdf');
174-
$pdf->background('/path/back.pdf')
189+
$result = $pdf->background('/path/back.pdf')
175190
->saveAs('/path/watermarked.pdf');
191+
if ($result === false) {
192+
$error = $pdf->getError();
193+
}
176194

177195
// Set background from another PDF (one page each)
178196
$pdf = new Pdf('/path/my.pdf');
179-
$pdf->multiBackground('/path/back_pages.pdf')
197+
$result = $pdf->multiBackground('/path/back_pages.pdf')
180198
->saveAs('/path/watermarked.pdf');
199+
if ($result === false) {
200+
$error = $pdf->getError();
201+
}
181202
```
182203

183204
#### Add overlay PDF
@@ -189,13 +210,19 @@ use mikehaertl\pdftk\Pdf;
189210

190211
// Stamp with another PDF (first page repeated)
191212
$pdf = new Pdf('/path/my.pdf');
192-
$pdf->stamp('/path/overlay.pdf')
213+
$result = $pdf->stamp('/path/overlay.pdf')
193214
->saveAs('/path/stamped.pdf');
215+
if ($result === false) {
216+
$error = $pdf->getError();
217+
}
194218

195219
// Stamp with another PDF (one page each)
196220
$pdf = new Pdf('/path/my.pdf');
197-
$pdf->multiStamp('/path/overlay_pages.pdf')
221+
$result = $pdf->multiStamp('/path/overlay_pages.pdf')
198222
->saveAs('/path/stamped.pdf');
223+
if ($result === false) {
224+
$error = $pdf->getError();
225+
}
199226
```
200227

201228
#### Unpack Files
@@ -206,7 +233,10 @@ Copy file attachments from a PDF to the given directory.
206233
use mikehaertl\pdftk\Pdf;
207234

208235
$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+
}
210240
```
211241

212242
#### Generate FDF
@@ -218,7 +248,10 @@ use mikehaertl\pdftk\Pdf;
218248

219249
// Create FDF from PDF
220250
$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+
}
222255
```
223256

224257
#### Get PDF data
@@ -231,10 +264,16 @@ use mikehaertl\pdftk\Pdf;
231264
// Get data
232265
$pdf = new Pdf('/path/my.pdf');
233266
$data = $pdf->getData();
267+
if ($data === false) {
268+
$error = $pdf->getError();
269+
}
234270

235271
// Get form data fields
236272
$pdf = new Pdf('/path/my.pdf');
237273
$data = $pdf->getDataFields();
274+
if ($data === false) {
275+
$error = $pdf->getError();
276+
}
238277

239278
// Get data as string
240279
echo $data;
@@ -262,9 +301,12 @@ $pdf->cat(1, 5)
262301

263302
// We now use the above PDF as source file for a new PDF
264303
$pdf2 = new Pdf($pdf);
265-
$pdf2->fillForm(['name' => 'ÄÜÖ äüö мирано čárka'])
304+
$result = $pdf2->fillForm(['name' => 'ÄÜÖ äüö мирано čárka'])
266305
->needAppearances()
267306
->saveAs('/path/filled.pdf');
307+
if ($result === false) {
308+
$error = $pdf->getError();
309+
}
268310
```
269311

270312
### Options
@@ -276,7 +318,7 @@ use mikehaertl\pdftk\Pdf;
276318

277319
$pdf = new Pdf('/path/my.pdf');
278320

279-
$pdf->allow('AllFeatures') // Change permissions
321+
$result = $pdf->allow('AllFeatures') // Change permissions
280322
->flatten() // Merge form data into document (doesn't work well with UTF-8!)
281323
->compress($value) // Compress/Uncompress
282324
->keepId('first') // Keep first/last Id of combined files
@@ -287,18 +329,27 @@ $pdf->allow('AllFeatures') // Change permissions
287329
->setUserPassword($pw) // Set user password
288330
->passwordEncryption(128) // Set password encryption strength
289331
->saveAs('new.pdf');
332+
if ($result === false) {
333+
$error = $pdf->getError();
334+
}
290335

291336
// Example: Fill PDF form and merge form data into PDF
292337
// Fill form with data array
293-
$pdf = new Pdf('/path/form.pdf');
338+
$result = $pdf = new Pdf('/path/form.pdf');
294339
$pdf->fillForm(['name' => 'My Name'])
295340
->flatten()
296341
->saveAs('/path/filled.pdf');
342+
if ($result === false) {
343+
$error = $pdf->getError();
344+
}
297345

298346
// Example: Remove password from a PDF
299347
$pdf = new Pdf;
300-
$pdf->addFile('/path/my.pdf', null, 'some**password')
348+
$result = $pdf->addFile('/path/my.pdf', null, 'some**password')
301349
->saveAs('/path/new.pdf');
350+
if ($result === false) {
351+
$error = $pdf->getError();
352+
}
302353
```
303354

304355
### Shell Command
@@ -327,8 +378,11 @@ file but only need the binary PDF content:
327378
use mikehaertl\pdftk\Pdf;
328379

329380
$pdf = new Pdf('/path/my.pdf');
330-
$pdf->fillForm(['name' => 'My Name'])
381+
$result = $pdf->fillForm(['name' => 'My Name'])
331382
->execute();
383+
if ($result === false) {
384+
$error = $pdf->getError();
385+
}
332386
$content = file_get_contents( (string) $pdf->getTmpFile() );
333387
```
334388

0 commit comments

Comments
 (0)