Skip to content

Commit 507699a

Browse files
committed
Issue #263 Implement attachFiles()
1 parent 706e6e8 commit 507699a

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,35 @@ if ($result === false) {
261261
}
262262
```
263263

264+
#### Attach Files
265+
266+
Add file attachments to the document or to a specific page.
267+
268+
```php
269+
use mikehaertl\pdftk\Pdf;
270+
271+
$files = [
272+
'/path/to/file1',
273+
'/path/to/file2',
274+
]
275+
276+
// Add files at the document level
277+
$pdf = new Pdf('/path/my.pdf');
278+
$result = $pdf->attachFiles($files)
279+
->saveAs('/path/withfiles.pdf');
280+
if ($result === false) {
281+
$error = $pdf->getError();
282+
}
283+
284+
// Add files to a specific page
285+
$pdf = new Pdf('/path/my.pdf');
286+
$page = 7;
287+
$result = $pdf->attachFiles($files, $page)
288+
->saveAs('/path/withfiles.pdf');
289+
if ($result === false) {
290+
$error = $pdf->getError();
291+
}
292+
```
264293
#### Unpack Files
265294

266295
Copy file attachments from a PDF to the given directory.

src/Pdf.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,27 @@ public function burst($filepattern = null)
231231
return $this->execute();
232232
}
233233

234+
/**
235+
* Attach files to the PDF
236+
*
237+
* @param array $files the list of full paths to the files to attach
238+
* @param string $toPage the page to add the attachment to. If omitted the
239+
* files are attached at the document level.
240+
* @return bool whether the operation was successful
241+
*/
242+
public function attachFiles($files, $toPage = null)
243+
{
244+
$this->constrainSingleFile();
245+
if ($toPage !== null) {
246+
$files[] = 'to_page';
247+
$files[] = $toPage;
248+
}
249+
$this->getCommand()
250+
->setOperation('attach_files')
251+
->setOperationArgument($files, true);
252+
return $this;
253+
}
254+
234255
/**
235256
* Copy all attachments from the PDF to the given directory
236257
*

tests/PdfTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public function testCanShuffleFiles()
207207
$this->assertFileExists($file);
208208

209209
$tmpFile = (string) $pdf->getTmpFile();
210+
$this->assertFileExists($file);
210211
$this->assertEquals(
211212
"pdftk 'A'='$document1' 'B'='$document2' shuffle A1-5 2 3 4 Bend-2even A3-5east B4-8eveneast A1south 'output' '$tmpFile'",
212213
(string) $pdf->getCommand()
@@ -247,6 +248,46 @@ public function testCanBurstWithFilePattern()
247248
@unlink($dir . '/doc_data.txt');
248249
}
249250

251+
public function testAttachFiles()
252+
{
253+
$document = $this->getDocument1();
254+
$file = $this->getOutFile();
255+
256+
$attachment1 = __DIR__ . '/files/testfile1.txt';
257+
$attachment2 = __DIR__ . '/files/testfile2.txt';
258+
$attachments = array($attachment1, $attachment2);
259+
260+
$pdf = new Pdf($document);
261+
$this->assertInstanceOf('mikehaertl\pdftk\Pdf', $pdf->attachFiles($attachments));
262+
$this->assertTrue($pdf->saveAs($file));
263+
$this->assertFileExists($file);
264+
$tmpFile = (string) $pdf->getTmpFile();
265+
$this->assertEquals(
266+
"pdftk 'A'='$document' 'attach_files' '$attachment1' '$attachment2' 'output' '$tmpFile'",
267+
(string) $pdf->getCommand()
268+
);
269+
}
270+
271+
public function testAttachFilesToPage()
272+
{
273+
$document = $this->getDocument1();
274+
$file = $this->getOutFile();
275+
276+
$attachment1 = __DIR__ . '/files/testfile1.txt';
277+
$attachment2 = __DIR__ . '/files/testfile2.txt';
278+
$attachments = array($attachment1, $attachment2);
279+
280+
$pdf = new Pdf($document);
281+
$this->assertInstanceOf('mikehaertl\pdftk\Pdf', $pdf->attachFiles($attachments, 1));
282+
$this->assertTrue($pdf->saveAs($file));
283+
$this->assertFileExists($file);
284+
$tmpFile = (string) $pdf->getTmpFile();
285+
$this->assertEquals(
286+
"pdftk 'A'='$document' 'attach_files' '$attachment1' '$attachment2' 'to_page' '1' 'output' '$tmpFile'",
287+
(string) $pdf->getCommand()
288+
);
289+
}
290+
250291
public function testUnpackFiles()
251292
{
252293
$document = $this->getDocument1();

tests/files/testfile1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Testfile 1

tests/files/testfile2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Testfile 1

0 commit comments

Comments
 (0)