3 namespace Tests\Exports;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Exceptions\PdfExportException;
7 use BookStack\Exports\PdfGenerator;
8 use FilesystemIterator;
11 class PdfExportTest extends TestCase
13 public function test_page_pdf_export()
15 $page = $this->entities->page();
18 $resp = $this->get($page->getUrl('/export/pdf'));
19 $resp->assertStatus(200);
20 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
23 public function test_book_pdf_export()
25 $page = $this->entities->page();
29 $resp = $this->get($book->getUrl('/export/pdf'));
30 $resp->assertStatus(200);
31 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
34 public function test_chapter_pdf_export()
36 $chapter = $this->entities->chapter();
39 $resp = $this->get($chapter->getUrl('/export/pdf'));
40 $resp->assertStatus(200);
41 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
45 public function test_page_pdf_export_converts_iframes_to_links()
47 $page = Page::query()->first()->forceFill([
48 'html' => '<iframe width="560" height="315" src="//www.youtube.com/embed/ShqUjt33uOs"></iframe>',
53 $mockPdfGenerator = $this->mock(PdfGenerator::class);
54 $mockPdfGenerator->shouldReceive('fromHtml')
55 ->with(\Mockery::capture($pdfHtml))
57 $mockPdfGenerator->shouldReceive('getActiveEngine')->andReturn(PdfGenerator::ENGINE_DOMPDF);
59 $this->asEditor()->get($page->getUrl('/export/pdf'));
60 $this->assertStringNotContainsString('iframe>', $pdfHtml);
61 $this->assertStringContainsString('<p><a href="https://www.youtube.com/embed/ShqUjt33uOs">https://www.youtube.com/embed/ShqUjt33uOs</a></p>', $pdfHtml);
64 public function test_page_pdf_export_opens_details_blocks()
66 $page = $this->entities->page()->forceFill([
67 'html' => '<details><summary>Hello</summary><p>Content!</p></details>',
72 $mockPdfGenerator = $this->mock(PdfGenerator::class);
73 $mockPdfGenerator->shouldReceive('fromHtml')
74 ->with(\Mockery::capture($pdfHtml))
76 $mockPdfGenerator->shouldReceive('getActiveEngine')->andReturn(PdfGenerator::ENGINE_DOMPDF);
78 $this->asEditor()->get($page->getUrl('/export/pdf'));
79 $this->assertStringContainsString('<details open="open"', $pdfHtml);
82 public function test_wkhtmltopdf_only_used_when_allow_untrusted_is_true()
84 $page = $this->entities->page();
86 config()->set('exports.snappy.pdf_binary', '/abc123');
87 config()->set('app.allow_untrusted_server_fetching', false);
89 $resp = $this->asEditor()->get($page->getUrl('/export/pdf'));
90 $resp->assertStatus(200); // Sucessful response with invalid snappy binary indicates dompdf usage.
92 config()->set('app.allow_untrusted_server_fetching', true);
93 $resp = $this->get($page->getUrl('/export/pdf'));
94 $resp->assertStatus(500); // Bad response indicates wkhtml usage
97 public function test_pdf_command_option_used_if_set()
99 $page = $this->entities->page();
100 $command = 'cp {input_html_path} {output_pdf_path}';
101 config()->set('exports.pdf_command', $command);
103 $resp = $this->asEditor()->get($page->getUrl('/export/pdf'));
104 $download = $resp->getContent();
106 $this->assertStringContainsString(e($page->name), $download);
107 $this->assertStringContainsString('<html lang=', $download);
110 public function test_pdf_command_option_errors_if_output_path_not_written_to()
112 $page = $this->entities->page();
113 $command = 'echo "hi"';
114 config()->set('exports.pdf_command', $command);
116 $this->assertThrows(function () use ($page) {
117 $this->withoutExceptionHandling()->asEditor()->get($page->getUrl('/export/pdf'));
118 }, PdfExportException::class);
121 public function test_pdf_command_option_errors_if_command_returns_error_status()
123 $page = $this->entities->page();
125 config()->set('exports.pdf_command', $command);
127 $this->assertThrows(function () use ($page) {
128 $this->withoutExceptionHandling()->asEditor()->get($page->getUrl('/export/pdf'));
129 }, PdfExportException::class);
132 public function test_pdf_command_timeout_option_limits_export_time()
134 $page = $this->entities->page();
135 $command = 'php -r \'sleep(4);\'';
136 config()->set('exports.pdf_command', $command);
137 config()->set('exports.pdf_command_timeout', 1);
139 $this->assertThrows(function () use ($page) {
141 $this->withoutExceptionHandling()->asEditor()->get($page->getUrl('/export/pdf'));
143 $this->assertTrue(time() < ($start + 3));
144 }, PdfExportException::class,
145 "PDF Export via command failed due to timeout at 1 second(s)");
148 public function test_pdf_command_option_does_not_leave_temp_files()
150 $tempDir = sys_get_temp_dir();
151 $startTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
153 $page = $this->entities->page();
154 $command = 'cp {input_html_path} {output_pdf_path}';
155 config()->set('exports.pdf_command', $command);
157 $this->asEditor()->get($page->getUrl('/export/pdf'));
159 $afterTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
160 $this->assertEquals($startTempFileCount, $afterTempFileCount);