]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PdfGenerator.php
Queries: Update API to align data with previous versions
[bookstack] / app / Entities / Tools / PdfGenerator.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use Barryvdh\DomPDF\Facade\Pdf as DomPDF;
6 use Barryvdh\Snappy\Facades\SnappyPdf;
7
8 class PdfGenerator
9 {
10     const ENGINE_DOMPDF = 'dompdf';
11     const ENGINE_WKHTML = 'wkhtml';
12
13     /**
14      * Generate PDF content from the given HTML content.
15      */
16     public function fromHtml(string $html): string
17     {
18         if ($this->getActiveEngine() === self::ENGINE_WKHTML) {
19             $pdf = SnappyPDF::loadHTML($html);
20             $pdf->setOption('print-media-type', true);
21         } else {
22             $pdf = DomPDF::loadHTML($html);
23         }
24
25         return $pdf->output();
26     }
27
28     /**
29      * Get the currently active PDF engine.
30      * Returns the value of an `ENGINE_` const on this class.
31      */
32     public function getActiveEngine(): string
33     {
34         $useWKHTML = config('snappy.pdf.binary') !== false && config('app.allow_untrusted_server_fetching') === true;
35
36         return $useWKHTML ? self::ENGINE_WKHTML : self::ENGINE_DOMPDF;
37     }
38 }