1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

PDF: Removed barryvdh snappy to use snappy direct

Also simplifies config format, and updates snappy implmentation to use
the new config file.
Not yet tested.
This commit is contained in:
Dan Brown
2024-04-24 15:13:44 +01:00
parent bb6670d395
commit 40200856af
6 changed files with 28 additions and 108 deletions

View File

@ -116,7 +116,6 @@ return [
// Application Service Providers
'providers' => ServiceProvider::defaultProviders()->merge([
// Third party service providers
Barryvdh\Snappy\ServiceProvider::class,
SocialiteProviders\Manager\ServiceProvider::class,
// BookStack custom service providers

View File

@ -31,22 +31,11 @@ return [
// 2024-04: Snappy/WKHTMLtoPDF now considered deprecated in regard to BookStack support.
'snappy' => [
'pdf' => [
'enabled' => true,
'binary' => file_exists(base_path('wkhtmltopdf')) ? base_path('wkhtmltopdf') : env('WKHTMLTOPDF', false),
'timeout' => false,
'options' => [
'outline' => true,
'page-size' => $snappyPaperSizeMap[$exportPageSize] ?? 'A4',
],
'env' => [],
],
'image' => [
'enabled' => false,
'binary' => '/usr/local/bin/wkhtmltoimage',
'timeout' => false,
'options' => [],
'env' => [],
'pdf_binary' => env('WKHTMLTOPDF', false),
'options' => [
'print-media-type' => true,
'outline' => true,
'page-size' => $snappyPaperSizeMap[$exportPageSize] ?? 'A4',
],
],

View File

@ -2,7 +2,7 @@
namespace BookStack\Entities\Tools;
use Barryvdh\Snappy\Facades\SnappyPdf;
use Knp\Snappy\Pdf as SnappyPdf;
use Dompdf\Dompdf;
class PdfGenerator
@ -19,9 +19,7 @@ class PdfGenerator
$engine = $this->getActiveEngine();
if ($engine === self::ENGINE_WKHTML) {
$pdf = SnappyPDF::loadHTML($html);
$pdf->setOption('print-media-type', true);
return $pdf->output();
return $this->renderUsingWkhtml($html);
} else if ($engine === self::ENGINE_COMMAND) {
// TODO - Support PDF command
return '';
@ -36,18 +34,23 @@ class PdfGenerator
*/
public function getActiveEngine(): string
{
$wkhtmlBinaryPath = config('snappy.pdf.binary');
if (file_exists(base_path('wkhtmltopdf'))) {
$wkhtmlBinaryPath = base_path('wkhtmltopdf');
}
if (is_string($wkhtmlBinaryPath) && config('app.allow_untrusted_server_fetching') === true) {
if ($this->getWkhtmlBinaryPath() && config('app.allow_untrusted_server_fetching') === true) {
return self::ENGINE_WKHTML;
}
return self::ENGINE_DOMPDF;
}
protected function getWkhtmlBinaryPath(): string
{
$wkhtmlBinaryPath = config('exports.snappy.pdf_binary');
if (file_exists(base_path('wkhtmltopdf'))) {
$wkhtmlBinaryPath = base_path('wkhtmltopdf');
}
return $wkhtmlBinaryPath ?: '';
}
protected function renderUsingDomPdf(string $html): string
{
$options = config('exports.dompdf');
@ -60,6 +63,13 @@ class PdfGenerator
return (string) $domPdf->output();
}
protected function renderUsingWkhtml(string $html): string
{
$snappy = new SnappyPdf($this->getWkhtmlBinaryPath());
$options = config('exports.snappy.options');
return $snappy->getOutputFromHtml($html, $options);
}
/**
* Taken from https://github.com/barryvdh/laravel-dompdf/blob/v2.1.1/src/PDF.php
* Copyright (c) 2021 barryvdh, MIT License