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