Skip to content

Commit 7e31725

Browse files
committed
Exports: Improved PDF command temp file cleanup
1 parent 6d7ff59 commit 7e31725

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

app/Exports/PdfGenerator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,28 @@ protected function renderUsingCommand(string $html): string
9090
$process = Process::fromShellCommandline($command);
9191
$process->setTimeout($timeout);
9292

93+
$cleanup = function () use ($inputHtml, $outputPdf) {
94+
foreach ([$inputHtml, $outputPdf] as $file) {
95+
if (file_exists($file)) {
96+
unlink($file);
97+
}
98+
}
99+
};
100+
93101
try {
94102
$process->run();
95103
} catch (ProcessTimedOutException $e) {
104+
$cleanup();
96105
throw new PdfExportException("PDF Export via command failed due to timeout at {$timeout} second(s)");
97106
}
98107

99108
if (!$process->isSuccessful()) {
109+
$cleanup();
100110
throw new PdfExportException("PDF Export via command failed with exit code {$process->getExitCode()}, stdout: {$process->getOutput()}, stderr: {$process->getErrorOutput()}");
101111
}
102112

103113
$pdfContents = file_get_contents($outputPdf);
104-
unlink($outputPdf);
114+
$cleanup();
105115

106116
if ($pdfContents === false) {
107117
throw new PdfExportException("PDF Export via command failed, unable to read PDF output file");

tests/Exports/PdfExportTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Entities\Models\Page;
66
use BookStack\Exceptions\PdfExportException;
77
use BookStack\Exports\PdfGenerator;
8+
use FilesystemIterator;
89
use Tests\TestCase;
910

1011
class PdfExportTest extends TestCase
@@ -128,7 +129,7 @@ public function test_pdf_command_option_errors_if_command_returns_error_status()
128129
}, PdfExportException::class);
129130
}
130131

131-
public function test_pdf_command_timout_option_limits_export_time()
132+
public function test_pdf_command_timeout_option_limits_export_time()
132133
{
133134
$page = $this->entities->page();
134135
$command = 'php -r \'sleep(4);\'';
@@ -143,4 +144,19 @@ public function test_pdf_command_timout_option_limits_export_time()
143144
}, PdfExportException::class,
144145
"PDF Export via command failed due to timeout at 1 second(s)");
145146
}
147+
148+
public function test_pdf_command_option_does_not_leave_temp_files()
149+
{
150+
$tempDir = sys_get_temp_dir();
151+
$startTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
152+
153+
$page = $this->entities->page();
154+
$command = 'cp {input_html_path} {output_pdf_path}';
155+
config()->set('exports.pdf_command', $command);
156+
157+
$this->asEditor()->get($page->getUrl('/export/pdf'));
158+
159+
$afterTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
160+
$this->assertEquals($startTempFileCount, $afterTempFileCount);
161+
}
146162
}

0 commit comments

Comments
 (0)