]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PdfGenerator.php
PDF: Added tests for pdf command, fixed old tests for changes
[bookstack] / app / Entities / Tools / PdfGenerator.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Exceptions\PdfExportException;
6 use Knp\Snappy\Pdf as SnappyPdf;
7 use Dompdf\Dompdf;
8 use Symfony\Component\Process\Process;
9
10 class PdfGenerator
11 {
12     const ENGINE_DOMPDF = 'dompdf';
13     const ENGINE_WKHTML = 'wkhtml';
14     const ENGINE_COMMAND = 'command';
15
16     /**
17      * Generate PDF content from the given HTML content.
18      * @throws PdfExportException
19      */
20     public function fromHtml(string $html): string
21     {
22         return match ($this->getActiveEngine()) {
23             self::ENGINE_COMMAND => $this->renderUsingCommand($html),
24             self::ENGINE_WKHTML => $this->renderUsingWkhtml($html),
25             default => $this->renderUsingDomPdf($html)
26         };
27     }
28
29     /**
30      * Get the currently active PDF engine.
31      * Returns the value of an `ENGINE_` const on this class.
32      */
33     public function getActiveEngine(): string
34     {
35         if (config('exports.pdf_command')) {
36             return self::ENGINE_COMMAND;
37         }
38
39         if ($this->getWkhtmlBinaryPath() && config('app.allow_untrusted_server_fetching') === true) {
40             return self::ENGINE_WKHTML;
41         }
42
43         return self::ENGINE_DOMPDF;
44     }
45
46     protected function getWkhtmlBinaryPath(): string
47     {
48         $wkhtmlBinaryPath = config('exports.snappy.pdf_binary');
49         if (file_exists(base_path('wkhtmltopdf'))) {
50             $wkhtmlBinaryPath = base_path('wkhtmltopdf');
51         }
52
53         return $wkhtmlBinaryPath ?: '';
54     }
55
56     protected function renderUsingDomPdf(string $html): string
57     {
58         $options = config('exports.dompdf');
59         $domPdf = new Dompdf($options);
60         $domPdf->setBasePath(base_path('public'));
61
62         $domPdf->loadHTML($this->convertEntities($html));
63         $domPdf->render();
64
65         return (string) $domPdf->output();
66     }
67
68     /**
69      * @throws PdfExportException
70      */
71     protected function renderUsingCommand(string $html): string
72     {
73         $command = config('exports.pdf_command');
74         $inputHtml = tempnam(sys_get_temp_dir(), 'bs-pdfgen-html-');
75         $outputPdf = tempnam(sys_get_temp_dir(), 'bs-pdfgen-output-');
76
77         $replacementsByPlaceholder = [
78             '{input_html_path}' => $inputHtml,
79             '{output_pdf_path}' => $outputPdf,
80         ];
81
82         foreach ($replacementsByPlaceholder as $placeholder => $replacement) {
83             $command = str_replace($placeholder, escapeshellarg($replacement), $command);
84         }
85
86         file_put_contents($inputHtml, $html);
87
88         $process = Process::fromShellCommandline($command);
89         $process->setTimeout(15);
90         $process->run();
91
92         if (!$process->isSuccessful()) {
93             throw new PdfExportException("PDF Export via command failed with exit code {$process->getExitCode()}, stdout: {$process->getOutput()}, stderr: {$process->getErrorOutput()}");
94         }
95
96         $pdfContents = file_get_contents($outputPdf);
97         unlink($outputPdf);
98
99         if ($pdfContents === false) {
100             throw new PdfExportException("PDF Export via command failed, unable to read PDF output file");
101         } else if (empty($pdfContents)) {
102             throw new PdfExportException("PDF Export via command failed, PDF output file is empty");
103         }
104
105         return $pdfContents;
106     }
107
108     protected function renderUsingWkhtml(string $html): string
109     {
110         $snappy = new SnappyPdf($this->getWkhtmlBinaryPath());
111         $options = config('exports.snappy.options');
112         return $snappy->getOutputFromHtml($html, $options);
113     }
114
115     /**
116      * Taken from https://github.com/barryvdh/laravel-dompdf/blob/v2.1.1/src/PDF.php
117      * Copyright (c) 2021 barryvdh, MIT License
118      * https://github.com/barryvdh/laravel-dompdf/blob/v2.1.1/LICENSE
119      */
120     protected function convertEntities(string $subject): string
121     {
122         $entities = [
123             '€' => '&euro;',
124             '£' => '&pound;',
125         ];
126
127         foreach ($entities as $search => $replace) {
128             $subject = str_replace($search, $replace, $subject);
129         }
130         return $subject;
131     }
132 }