From 40200856af366f735283da4cc1b28519ddb3586b Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Wed, 24 Apr 2024 15:13:44 +0100 Subject: [PATCH] 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. --- app/Config/app.php | 1 - app/Config/exports.php | 21 ++------ app/Entities/Tools/PdfGenerator.php | 30 +++++++---- composer.json | 2 +- composer.lock | 80 +---------------------------- readme.md | 2 +- 6 files changed, 28 insertions(+), 108 deletions(-) diff --git a/app/Config/app.php b/app/Config/app.php index 67f31159f..b96d0bdb7 100644 --- a/app/Config/app.php +++ b/app/Config/app.php @@ -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 diff --git a/app/Config/exports.php b/app/Config/exports.php index 63cc2419d..88dc08cba 100644 --- a/app/Config/exports.php +++ b/app/Config/exports.php @@ -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', ], ], diff --git a/app/Entities/Tools/PdfGenerator.php b/app/Entities/Tools/PdfGenerator.php index 7502c10ff..e187b9ab2 100644 --- a/app/Entities/Tools/PdfGenerator.php +++ b/app/Entities/Tools/PdfGenerator.php @@ -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 diff --git a/composer.json b/composer.json index 94f64ec72..b90ab224e 100644 --- a/composer.json +++ b/composer.json @@ -17,11 +17,11 @@ "ext-mbstring": "*", "ext-xml": "*", "bacon/bacon-qr-code": "^2.0", - "barryvdh/laravel-snappy": "^1.0", "doctrine/dbal": "^3.5", "dompdf/dompdf": "^2.0", "guzzlehttp/guzzle": "^7.4", "intervention/image": "^3.5", + "knplabs/knp-snappy": "^1.5", "laravel/framework": "^10.10", "laravel/socialite": "^5.10", "laravel/tinker": "^2.8", diff --git a/composer.lock b/composer.lock index 657a5a7fb..ad5648d6b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c0c5a3169cb23d9ab8e34324202d4c37", + "content-hash": "97259e40ffe5518cfcdf1e32eacbb175", "packages": [ { "name": "aws/aws-crt-php", @@ -209,84 +209,6 @@ }, "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", "version": "0.11.0", diff --git a/readme.md b/readme.md index 5adcc06bb..c46e1641f 100644 --- a/readme.md +++ b/readme.md @@ -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)_ * [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)_ -* [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)_ * [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)_