3 namespace BookStack\Entities\Tools;
5 use BookStack\Exceptions\PdfExportException;
6 use Knp\Snappy\Pdf as SnappyPdf;
8 use Symfony\Component\Process\Process;
12 const ENGINE_DOMPDF = 'dompdf';
13 const ENGINE_WKHTML = 'wkhtml';
14 const ENGINE_COMMAND = 'command';
17 * Generate PDF content from the given HTML content.
18 * @throws PdfExportException
20 public function fromHtml(string $html): string
22 return match ($this->getActiveEngine()) {
23 self::ENGINE_COMMAND => $this->renderUsingCommand($html),
24 self::ENGINE_WKHTML => $this->renderUsingWkhtml($html),
25 default => $this->renderUsingDomPdf($html)
30 * Get the currently active PDF engine.
31 * Returns the value of an `ENGINE_` const on this class.
33 public function getActiveEngine(): string
35 if (config('exports.pdf_command')) {
36 return self::ENGINE_COMMAND;
39 if ($this->getWkhtmlBinaryPath() && config('app.allow_untrusted_server_fetching') === true) {
40 return self::ENGINE_WKHTML;
43 return self::ENGINE_DOMPDF;
46 protected function getWkhtmlBinaryPath(): string
48 $wkhtmlBinaryPath = config('exports.snappy.pdf_binary');
49 if (file_exists(base_path('wkhtmltopdf'))) {
50 $wkhtmlBinaryPath = base_path('wkhtmltopdf');
53 return $wkhtmlBinaryPath ?: '';
56 protected function renderUsingDomPdf(string $html): string
58 $options = config('exports.dompdf');
59 $domPdf = new Dompdf($options);
60 $domPdf->setBasePath(base_path('public'));
62 $domPdf->loadHTML($this->convertEntities($html));
65 return (string) $domPdf->output();
69 * @throws PdfExportException
71 protected function renderUsingCommand(string $html): string
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-');
77 $replacementsByPlaceholder = [
78 '{input_html_path}' => $inputHtml,
79 '{output_pdf_path}' => $outputPdf,
82 foreach ($replacementsByPlaceholder as $placeholder => $replacement) {
83 $command = str_replace($placeholder, escapeshellarg($replacement), $command);
86 file_put_contents($inputHtml, $html);
88 $process = Process::fromShellCommandline($command);
89 $process->setTimeout(15);
92 if (!$process->isSuccessful()) {
93 throw new PdfExportException("PDF Export via command failed with exit code {$process->getExitCode()}, stdout: {$process->getOutput()}, stderr: {$process->getErrorOutput()}");
96 $pdfContents = file_get_contents($outputPdf);
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");
108 protected function renderUsingWkhtml(string $html): string
110 $snappy = new SnappyPdf($this->getWkhtmlBinaryPath());
111 $options = config('exports.snappy.options');
112 return $snappy->getOutputFromHtml($html, $options);
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
120 protected function convertEntities(string $subject): string
127 foreach ($entities as $search => $replace) {
128 $subject = str_replace($search, $replace, $subject);