Skip to content

Commit

Permalink
Merge pull request #29 from beganovich/delete-after-creation
Browse files Browse the repository at this point in the history
Clean HTML files with temporary ones
  • Loading branch information
beganovich authored Sep 14, 2022
2 parents 8836144 + cad8aea commit b2656c2
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Snappdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,17 @@ public function getKeepTemporaryFiles(): bool
return (bool) $this->keepTemporaryFiles;
}

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

unlink($tempFile);

if ($content['type'] === 'html') {
unlink($content['content']);
}
}

public function generate(): ?string
Expand Down Expand Up @@ -245,7 +249,7 @@ public function generate(): ?string
$platform = (new DownloadChromiumCommand())->generatePlatformCode();

if ($platform == 'Win' || $platform == 'Win_x64') {
return $this->executeOnWindows($commandInput, $pdf);
return $this->executeOnWindows($commandInput, $pdf, $content);
}

$process = new Process($commandInput);
Expand All @@ -256,11 +260,11 @@ public function generate(): ?string
throw new \Symfony\Component\Process\Exception\ProcessFailedException($process);
}

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

$this->cleanup($pdf);
$this->cleanup($pdf, $content);

return $content;
return $pdfContent;
}

public function save(string $path): void
Expand All @@ -272,7 +276,7 @@ public function save(string $path): void
$filesystem->appendToFile($path, $pdf);
}

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

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

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

$this->cleanup($pdf);
$this->cleanup($pdf, $content);

return file_get_contents($content);
return file_get_contents($pdfContent);
}
}

0 comments on commit b2656c2

Please sign in to comment.