Skip to content

Commit

Permalink
Merge pull request #28 from beganovich/delete-after-creation
Browse files Browse the repository at this point in the history
Cleaning the temporary files
  • Loading branch information
beganovich authored Sep 14, 2022
2 parents 7964d03 + e39b41f commit 8836144
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ A simple library that lets you convert webpages or HTML into PDF files using Chr
- [Headless Chrome doesn't launch on UNIX](#headless-chrome-doesnt-launch-on-unix)
- [Comparison to Browsershot](#comparison-to-browsershot)
- [Delay loading](#delay-loading)
- [Temporary files](#temporary-files)
- [Credits](#credits)
- [Licence](#licence)

Expand Down Expand Up @@ -338,6 +339,21 @@ printing.
TLDR; If you set delay loading to 10 seconds & Ajax call takes 2 seconds to complete, PDF rendering will start
immediately after Ajax call is completed (after 2 seconds), and it won't wait 10 seconds.

### Temporary files
Starting with version 3, snappdf will automatically get rid of temporary files. If you still want to keep them, you can do it using `setKeepTemporaryFiles` method.

```php
$snappdf = new \Beganovich\Snappdf\Snappdf();

$pdf = $snappdf
->setUrl('https://github.com')
->setChromiumPath('/path/to/your/chrome')
->setKeepTemporaryFiles(true)
->generate();

file_put_contents('my.pdf', $pdf);
```

## Credits

- [David Bomba](https://github.com/turbo124)
Expand Down
44 changes: 38 additions & 6 deletions src/Snappdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Snappdf

private $waitBeforePrinting;

private $keepTemporaryFiles = false;

public function __construct()
{
$this->chromiumArguments = [
Expand Down Expand Up @@ -125,11 +127,11 @@ public function addChromiumArguments(string $chromiumArgument): self

foreach ($arguments as $argument) {
$arg = explode('=', $argument);
$matches = preg_grep('/'.$arg[0].'(.*)/', $this->chromiumArguments);
$matches = preg_grep('/' . $arg[0] . '(.*)/', $this->chromiumArguments);

if (count($matches) > 0) {
$this->chromiumArguments = preg_replace(
'/'.$arg[0].'(.*)/',
'/' . $arg[0] . '(.*)/',
$argument,
$this->chromiumArguments
);
Expand Down Expand Up @@ -179,6 +181,27 @@ public function getWaitTime(): ?int
return $this->waitBeforePrinting;
}

public function setKeepTemporaryFiles(bool $keep): self
{
$this->keepTemporaryFiles = $keep;

return $this;
}

public function getKeepTemporaryFiles(): bool
{
return (bool) $this->keepTemporaryFiles;
}

private function cleanup(string $tempFile): void
{
if ($this->keepTemporaryFiles) {
return;
}

unlink($tempFile);
}

public function generate(): ?string
{
$content = [
Expand Down Expand Up @@ -207,7 +230,8 @@ public function generate(): ?string
$pdf = tempnam(sys_get_temp_dir(), 'pdf_');
rename($pdf, $pdf .= '.pdf');

$commandInput = [ $this->getChromiumPath() ];
$commandInput = [$this->getChromiumPath()];

foreach ($this->getChromiumArguments() as $argument) {
array_push($commandInput, $argument);
}
Expand All @@ -232,7 +256,11 @@ public function generate(): ?string
throw new \Symfony\Component\Process\Exception\ProcessFailedException($process);
}

return file_get_contents($pdf);
$content = file_get_contents($pdf);

$this->cleanup($pdf);

return $content;
}

public function save(string $path): void
Expand All @@ -246,7 +274,7 @@ public function save(string $path): void

private function executeOnWindows(array $commands, $pdf): ?string
{
$command = implode(' ', $commands).' 2>&1'; // must add 2>&1 to redirect stderr to stdout // see https://stackoverflow.com/a/16665146/7511165
$command = implode(' ', $commands) . ' 2>&1'; // must add 2>&1 to redirect stderr to stdout // see https://stackoverflow.com/a/16665146/7511165

exec($command, $output, $statusCode);

Expand All @@ -257,6 +285,10 @@ private function executeOnWindows(array $commands, $pdf): ?string
throw new \Beganovich\Snappdf\Exception\ProcessFailedException($message);
}

return file_get_contents($pdf);
$content = file_get_contents($pdf);

$this->cleanup($pdf);

return file_get_contents($content);
}
}

0 comments on commit 8836144

Please sign in to comment.