Skip to content

Commit 7c021e6

Browse files
committed
Issue #197 Implement unpack_files operation
1 parent 45103cc commit 7c021e6

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ $pdf->multiStamp('/path/overlay_pages.pdf')
198198
->saveAs('/path/stamped.pdf');
199199
```
200200

201+
#### Unpack Files
202+
203+
Copy file attachments from a PDF to the given directory.
204+
205+
```php
206+
use mikehaertl\pdftk\Pdf;
207+
208+
$pdf = new Pdf('/path/my.pdf');
209+
$pdf->unpackFiles('/path/to/dir');
210+
```
211+
201212
#### Generate FDF
202213

203214
Create a FDF file from a given filled PDF form.

src/Pdf.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ public function shuffle($start, $end = null, $handle = null, $qualifier = null,
215215
*
216216
* @param string|null $filepattern the output name in sprintf format or
217217
* null for default 'pg_%04d.pdf'
218-
* @return bool whether the burst command was successful
219218
* @return bool whether the burst operation was successful
220219
*/
221220
public function burst($filepattern = null)
@@ -226,6 +225,20 @@ public function burst($filepattern = null)
226225
return $this->execute();
227226
}
228227

228+
/**
229+
* Copy all attachments from the PDF to the given directory
230+
*
231+
* @param string|null $dir the output directory
232+
* @return bool whether the operation was successful
233+
*/
234+
public function unpackFiles($dir = null)
235+
{
236+
$this->constrainSingleFile();
237+
$this->getCommand()->setOperation('unpack_files');
238+
$this->_output = $dir;
239+
return $this->execute();
240+
}
241+
229242
/**
230243
* Generate the FDF file for a single PDF file.
231244
*

tests/PdfTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,19 @@ public function testCanBurstWithFilePattern()
259259
@unlink($dir . '/doc_data.txt');
260260
}
261261

262+
public function testUnpackFiles()
263+
{
264+
$document = $this->getDocument1();
265+
$dir = __DIR__;
266+
267+
$pdf = new Pdf($document);
268+
$this->assertTrue($pdf->unpackFiles($dir));
269+
$this->assertEquals(
270+
"pdftk 'A'='$document' unpack_files 'output' '$dir'",
271+
(string) $pdf->getCommand()
272+
);
273+
}
274+
262275
public function testCanCreateFdfFileFromPdf()
263276
{
264277
$form = $this->getFilledForm();

0 commit comments

Comments
 (0)