2021-11-25 10:12:32 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools;
|
|
|
|
|
2022-07-17 09:01:59 -04:00
|
|
|
use Barryvdh\DomPDF\Facade\Pdf as DomPDF;
|
2021-11-28 16:01:35 -05:00
|
|
|
use Barryvdh\Snappy\Facades\SnappyPdf;
|
2021-11-25 10:12:32 -05:00
|
|
|
|
|
|
|
class PdfGenerator
|
|
|
|
{
|
2022-01-24 12:24:00 -05:00
|
|
|
const ENGINE_DOMPDF = 'dompdf';
|
|
|
|
const ENGINE_WKHTML = 'wkhtml';
|
|
|
|
|
2021-11-25 10:12:32 -05:00
|
|
|
/**
|
|
|
|
* Generate PDF content from the given HTML content.
|
|
|
|
*/
|
|
|
|
public function fromHtml(string $html): string
|
|
|
|
{
|
2022-01-24 12:24:00 -05:00
|
|
|
if ($this->getActiveEngine() === self::ENGINE_WKHTML) {
|
2021-11-25 10:12:32 -05:00
|
|
|
$pdf = SnappyPDF::loadHTML($html);
|
|
|
|
$pdf->setOption('print-media-type', true);
|
|
|
|
} else {
|
|
|
|
$pdf = DomPDF::loadHTML($html);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pdf->output();
|
|
|
|
}
|
2022-01-24 12:24:00 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the currently active PDF engine.
|
|
|
|
* Returns the value of an `ENGINE_` const on this class.
|
|
|
|
*/
|
|
|
|
public function getActiveEngine(): string
|
|
|
|
{
|
|
|
|
$useWKHTML = config('snappy.pdf.binary') !== false && config('app.allow_untrusted_server_fetching') === true;
|
2022-01-24 15:55:03 -05:00
|
|
|
|
2022-01-24 12:24:00 -05:00
|
|
|
return $useWKHTML ? self::ENGINE_WKHTML : self::ENGINE_DOMPDF;
|
|
|
|
}
|
2021-11-28 16:01:35 -05:00
|
|
|
}
|