From cad8aeaf1c8426907706d7271d3b41f181e61056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 14 Sep 2022 12:33:54 +0200 Subject: [PATCH] Clean HTML files with temporary ones --- src/Snappdf.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Snappdf.php b/src/Snappdf.php index b54afba..db199bd 100644 --- a/src/Snappdf.php +++ b/src/Snappdf.php @@ -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 @@ -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); @@ -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 @@ -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 @@ -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); } }