]> BookStack Code Mirror - bookstack/commitdiff
PDF: Removed barryvdh snappy to use snappy direct
authorDan Brown <redacted>
Wed, 24 Apr 2024 14:13:44 +0000 (15:13 +0100)
committerDan Brown <redacted>
Wed, 24 Apr 2024 14:13:44 +0000 (15:13 +0100)
Also simplifies config format, and updates snappy implmentation to use
the new config file.
Not yet tested.

app/Config/app.php
app/Config/exports.php
app/Entities/Tools/PdfGenerator.php
composer.json
composer.lock
readme.md

index 67f31159f60c03e0f5730dda376dc6e262677920..b96d0bdb7885215ff65ac665b77591f37a23745d 100644 (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
index 63cc2419d3b56ad53037f5214d9a3c4772bdc7ac..88dc08cba354ca0922631b6d0e99bebb770a2558 100644 (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',
         ],
     ],
 
index 7502c10ff43243488c01e7cea82028def07c740f..e187b9ab2c4e50d892c40de70588c54586082939 100644 (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
index 94f64ec724578e1ba20fff3bdc6df8d7dd6de292..b90ab224eaa5b63b19d63c0614cd48b222f8e205 100644 (file)
         "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",
index 657a5a7fb78e4e1a394a6f42b4137073058b9213..ad5648d6bc3889a94d13e1dccef0c7a46e6ffc71 100644 (file)
@@ -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",
             },
             "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": "[email protected]"
-                }
-            ],
-            "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",
index 5adcc06bbcc7918dbead02f488c83c3a037fb062..c46e1641f19b8c9399f8669dfbd6e694e6251886 100644 (file)
--- 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)_