3 namespace BookStack\Exports;
5 use BookStack\Exceptions\PdfExportException;
7 use Knp\Snappy\Pdf as SnappyPdf;
8 use Symfony\Component\Process\Exception\ProcessTimedOutException;
9 use Symfony\Component\Process\Process;
13 const ENGINE_DOMPDF = 'dompdf';
14 const ENGINE_WKHTML = 'wkhtml';
15 const ENGINE_COMMAND = 'command';
18 * Generate PDF content from the given HTML content.
19 * @throws PdfExportException
21 public function fromHtml(string $html): string
23 return match ($this->getActiveEngine()) {
24 self::ENGINE_COMMAND => $this->renderUsingCommand($html),
25 self::ENGINE_WKHTML => $this->renderUsingWkhtml($html),
26 default => $this->renderUsingDomPdf($html)
31 * Get the currently active PDF engine.
32 * Returns the value of an `ENGINE_` const on this class.
34 public function getActiveEngine(): string
36 if (config('exports.pdf_command')) {
37 return self::ENGINE_COMMAND;
40 if ($this->getWkhtmlBinaryPath() && config('app.allow_untrusted_server_fetching') === true) {
41 return self::ENGINE_WKHTML;
44 return self::ENGINE_DOMPDF;
47 protected function getWkhtmlBinaryPath(): string
49 $wkhtmlBinaryPath = config('exports.snappy.pdf_binary');
50 if (file_exists(base_path('wkhtmltopdf'))) {
51 $wkhtmlBinaryPath = base_path('wkhtmltopdf');
54 return $wkhtmlBinaryPath ?: '';
57 protected function renderUsingDomPdf(string $html): string
59 $options = config('exports.dompdf');
60 $domPdf = new Dompdf($options);
61 $domPdf->setBasePath(base_path('public'));
63 $domPdf->loadHTML($this->convertEntities($html));
66 return (string) $domPdf->output();
70 * @throws PdfExportException
72 protected function renderUsingCommand(string $html): string
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-');
78 $replacementsByPlaceholder = [
79 '{input_html_path}' => $inputHtml,
80 '{output_pdf_path}' => $outputPdf,
83 foreach ($replacementsByPlaceholder as $placeholder => $replacement) {
84 $command = str_replace($placeholder, escapeshellarg($replacement), $command);
87 file_put_contents($inputHtml, $html);
89 $timeout = intval(config('exports.pdf_command_timeout'));
90 $process = Process::fromShellCommandline($command);
91 $process->setTimeout($timeout);
95 } catch (ProcessTimedOutException $e) {
96 throw new PdfExportException("PDF Export via command failed due to timeout at {$timeout} second(s)");
99 if (!$process->isSuccessful()) {
100 throw new PdfExportException("PDF Export via command failed with exit code {$process->getExitCode()}, stdout: {$process->getOutput()}, stderr: {$process->getErrorOutput()}");
103 $pdfContents = file_get_contents($outputPdf);
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");
115 protected function renderUsingWkhtml(string $html): string
117 $snappy = new SnappyPdf($this->getWkhtmlBinaryPath());
118 $options = config('exports.snappy.options');
119 return $snappy->getOutputFromHtml($html, $options);
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
127 protected function convertEntities(string $subject): string
134 foreach ($entities as $search => $replace) {
135 $subject = str_replace($search, $replace, $subject);