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
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
6 changed files with 28 additions and 108 deletions

View File

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

View File

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

View File

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

View File

@ -17,11 +17,11 @@
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-xml": "*", "ext-xml": "*",
"bacon/bacon-qr-code": "^2.0", "bacon/bacon-qr-code": "^2.0",
"barryvdh/laravel-snappy": "^1.0",
"doctrine/dbal": "^3.5", "doctrine/dbal": "^3.5",
"dompdf/dompdf": "^2.0", "dompdf/dompdf": "^2.0",
"guzzlehttp/guzzle": "^7.4", "guzzlehttp/guzzle": "^7.4",
"intervention/image": "^3.5", "intervention/image": "^3.5",
"knplabs/knp-snappy": "^1.5",
"laravel/framework": "^10.10", "laravel/framework": "^10.10",
"laravel/socialite": "^5.10", "laravel/socialite": "^5.10",
"laravel/tinker": "^2.8", "laravel/tinker": "^2.8",

80
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "c0c5a3169cb23d9ab8e34324202d4c37", "content-hash": "97259e40ffe5518cfcdf1e32eacbb175",
"packages": [ "packages": [
{ {
"name": "aws/aws-crt-php", "name": "aws/aws-crt-php",
@ -209,84 +209,6 @@
}, },
"time": "2022-12-07T17:46:57+00:00" "time": "2022-12-07T17:46:57+00:00"
}, },
{
"name": "barryvdh/laravel-snappy",
"version": "v1.0.3",
"source": {
"type": "git",
"url": "https://github.com/barryvdh/laravel-snappy.git",
"reference": "716dcb6db24de4ce8e6ae5941cfab152af337ea0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/barryvdh/laravel-snappy/zipball/716dcb6db24de4ce8e6ae5941cfab152af337ea0",
"reference": "716dcb6db24de4ce8e6ae5941cfab152af337ea0",
"shasum": ""
},
"require": {
"illuminate/filesystem": "^9|^10|^11.0",
"illuminate/support": "^9|^10|^11.0",
"knplabs/knp-snappy": "^1.4.4",
"php": ">=7.2"
},
"require-dev": {
"orchestra/testbench": "^7|^8|^9.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
},
"laravel": {
"providers": [
"Barryvdh\\Snappy\\ServiceProvider"
],
"aliases": {
"PDF": "Barryvdh\\Snappy\\Facades\\SnappyPdf",
"SnappyImage": "Barryvdh\\Snappy\\Facades\\SnappyImage"
}
}
},
"autoload": {
"psr-4": {
"Barryvdh\\Snappy\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Barry vd. Heuvel",
"email": "barryvdh@gmail.com"
}
],
"description": "Snappy PDF/Image for Laravel",
"keywords": [
"image",
"laravel",
"pdf",
"snappy",
"wkhtmltoimage",
"wkhtmltopdf"
],
"support": {
"issues": "https://github.com/barryvdh/laravel-snappy/issues",
"source": "https://github.com/barryvdh/laravel-snappy/tree/v1.0.3"
},
"funding": [
{
"url": "https://fruitcake.nl",
"type": "custom"
},
{
"url": "https://github.com/barryvdh",
"type": "github"
}
],
"time": "2024-03-09T19:20:39+00:00"
},
{ {
"name": "brick/math", "name": "brick/math",
"version": "0.11.0", "version": "0.11.0",

View File

@ -142,7 +142,7 @@ Note: This is not an exhaustive list of all libraries and projects that would be
* [Google Material Icons](https://github.com/google/material-design-icons) - _[Apache-2.0](https://github.com/google/material-design-icons/blob/master/LICENSE)_ * [Google Material Icons](https://github.com/google/material-design-icons) - _[Apache-2.0](https://github.com/google/material-design-icons/blob/master/LICENSE)_
* [markdown-it](https://github.com/markdown-it/markdown-it) and [markdown-it-task-lists](https://github.com/revin/markdown-it-task-lists) - _[MIT](https://github.com/markdown-it/markdown-it/blob/master/LICENSE) and [ISC](https://github.com/revin/markdown-it-task-lists/blob/master/LICENSE)_ * [markdown-it](https://github.com/markdown-it/markdown-it) and [markdown-it-task-lists](https://github.com/revin/markdown-it-task-lists) - _[MIT](https://github.com/markdown-it/markdown-it/blob/master/LICENSE) and [ISC](https://github.com/revin/markdown-it-task-lists/blob/master/LICENSE)_
* [Dompdf](https://github.com/dompdf/dompdf) - _[LGPL v2.1](https://github.com/dompdf/dompdf/blob/master/LICENSE.LGPL)_ * [Dompdf](https://github.com/dompdf/dompdf) - _[LGPL v2.1](https://github.com/dompdf/dompdf/blob/master/LICENSE.LGPL)_
* [BarryVD/Snappy (WKHTML2PDF)](https://github.com/barryvdh/laravel-snappy) - _[MIT](https://github.com/barryvdh/laravel-snappy/blob/master/LICENSE)_ * [KnpLabs/snappy](https://github.com/KnpLabs/snappy) - _[MIT](https://github.com/KnpLabs/snappy/blob/master/LICENSE)_
* [WKHTMLtoPDF](http://wkhtmltopdf.org/index.html) - _[LGPL v3.0](https://github.com/wkhtmltopdf/wkhtmltopdf/blob/master/LICENSE)_ * [WKHTMLtoPDF](http://wkhtmltopdf.org/index.html) - _[LGPL v3.0](https://github.com/wkhtmltopdf/wkhtmltopdf/blob/master/LICENSE)_
* [diagrams.net](https://github.com/jgraph/drawio) - _[Embedded Version Terms](https://www.diagrams.net/trust/) / [Source Project - Apache-2.0](https://github.com/jgraph/drawio/blob/dev/LICENSE)_ * [diagrams.net](https://github.com/jgraph/drawio) - _[Embedded Version Terms](https://www.diagrams.net/trust/) / [Source Project - Apache-2.0](https://github.com/jgraph/drawio/blob/dev/LICENSE)_
* [OneLogin's SAML PHP Toolkit](https://github.com/onelogin/php-saml) - _[MIT](https://github.com/onelogin/php-saml/blob/master/LICENSE)_ * [OneLogin's SAML PHP Toolkit](https://github.com/onelogin/php-saml) - _[MIT](https://github.com/onelogin/php-saml/blob/master/LICENSE)_