Skip to content

Commit daf2037

Browse files
committed
Issue #150 Add tempDir option
1 parent 060f40d commit daf2037

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ A PDF conversion and form utility based on pdftk.
3131

3232
> **Note** If you're on Ubuntu you may want to install the version from `ppa:malteworld/ppa`.
3333
> The default packages seems to use snap an there have been reports about file permission
34-
> issues with this version.
34+
> issues with this version. See below for a workaround.
3535
3636
## Installation
3737

@@ -321,6 +321,16 @@ $pdf->fillForm(['name' => 'My Name'])
321321
$content = file_get_contents( (string) $pdf->getTmpFile() );
322322
```
323323

324+
If you have permission issues you may have to set a directory where your
325+
`pdftk` command can write to:
326+
327+
```php
328+
use mikehaertl\pdftk\Pdf;
329+
330+
$pdf = new Pdf('/path/my.pdf');
331+
$pdf->tempDir = '/home/john/temp';
332+
```
333+
324334
## API
325335

326336
Please consult the source files for a full documentation of each method.

src/Pdf.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class Pdf
2626
*/
2727
public $ignoreWarnings = false;
2828

29+
/**
30+
* @var null|string an optional directory where temporary files should be
31+
* created. If left empty the directory is autodetected.
32+
*/
33+
public $tempDir;
34+
2935
/**
3036
* @var File the temporary output file
3137
*/
@@ -608,7 +614,7 @@ public function getCommand()
608614
public function getTmpFile()
609615
{
610616
if ($this->_tmpFile === null) {
611-
$this->_tmpFile = new File('', '.pdf', self::TMP_PREFIX);
617+
$this->_tmpFile = new File('', '.pdf', self::TMP_PREFIX, $this->tempDir);
612618
}
613619
return $this->_tmpFile;
614620
}

tests/PdfTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,20 @@ public function testCanGetPdfContentAsString()
583583
$this->assertEquals(file_get_contents($file), $pdf->toString());
584584
}
585585

586+
public function testCanSetTemporaryDirectory()
587+
{
588+
$document = $this->getDocument1();
589+
$file = $this->getOutFile();
590+
$dir = dirname($file);
591+
592+
$pdf = new Pdf($document);
593+
$pdf->tempDir = $dir;
594+
$this->assertTrue($pdf->saveAs($file));
595+
596+
$tmpFile = (string) $pdf->getTmpFile();
597+
$this->assertStringStartsWith($dir, $tmpFile);
598+
}
599+
586600
protected function getDocument1()
587601
{
588602
return __DIR__ . '/files/document1.pdf';

0 commit comments

Comments
 (0)