]> BookStack Code Mirror - bookstack/commitdiff
Further adjusted linked image sizes on PDF export
authorDan Brown <redacted>
Mon, 24 Jan 2022 17:24:00 +0000 (17:24 +0000)
committerDan Brown <redacted>
Mon, 24 Jan 2022 17:24:00 +0000 (17:24 +0000)
Further fixes for #3120, Adds DOMPDF specific adjustments to prevent
full width linked images being cut-off as per last tweak.
This does not fix usage in smaller cases (tables) but tested on
master DOMPDF branch shows that will likely be fixed in next DOMPDF
upstream release.
DOMPDF fixes would break WKHTMLTOPDF presentation so system updated
to conditionally apply styles.

app/Entities/Tools/ExportFormatter.php
app/Entities/Tools/PdfGenerator.php
resources/views/common/export-styles.blade.php
resources/views/layouts/export.blade.php

index 7f377cadb9b06417e23e585fddd470e67d292129..f993d332d5ee0e5783e373d40caedee57d367482 100644 (file)
@@ -92,6 +92,7 @@ class ExportFormatter
         $html = view('pages.export', [
             'page'   => $page,
             'format' => 'pdf',
+            'engine' => $this->pdfGenerator->getActiveEngine(),
         ])->render();
 
         return $this->htmlToPdf($html);
@@ -113,6 +114,7 @@ class ExportFormatter
             'chapter' => $chapter,
             'pages'   => $pages,
             'format'  => 'pdf',
+            'engine'  => $this->pdfGenerator->getActiveEngine(),
         ])->render();
 
         return $this->htmlToPdf($html);
@@ -130,6 +132,7 @@ class ExportFormatter
             'book'         => $book,
             'bookChildren' => $bookTree,
             'format'       => 'pdf',
+            'engine'       => $this->pdfGenerator->getActiveEngine(),
         ])->render();
 
         return $this->htmlToPdf($html);
index a14f29d4b509977f370e82b3fc76c94878b2e9c2..c2e8f3d4c94143b932f112f790aac0650eb985c3 100644 (file)
@@ -7,14 +7,15 @@ use Barryvdh\Snappy\Facades\SnappyPdf;
 
 class PdfGenerator
 {
+    const ENGINE_DOMPDF = 'dompdf';
+    const ENGINE_WKHTML = 'wkhtml';
+
     /**
      * Generate PDF content from the given HTML content.
      */
     public function fromHtml(string $html): string
     {
-        $useWKHTML = config('snappy.pdf.binary') !== false && config('app.allow_untrusted_server_fetching') === true;
-
-        if ($useWKHTML) {
+        if ($this->getActiveEngine() === self::ENGINE_WKHTML) {
             $pdf = SnappyPDF::loadHTML($html);
             $pdf->setOption('print-media-type', true);
         } else {
@@ -23,4 +24,14 @@ class PdfGenerator
 
         return $pdf->output();
     }
+
+    /**
+     * 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;
+        return $useWKHTML ? self::ENGINE_WKHTML : self::ENGINE_DOMPDF;
+    }
 }
index ae26613b199eae5f128f052e67911a3bf58d8e37..ee10637dddf4e0a0022f2feb4e78bc08d38a165f 100644 (file)
             display: block;
         }
 
+        @if($engine === \BookStack\Entities\Tools\PdfGenerator::ENGINE_DOMPDF)
+        {{-- Fix for full width linked image sizes on DOMPDF --}}
         .page-content a > img {
-            max-width: none;
+            max-width: 700px;
         }
+        {{-- Undoes the above for table images to prevent visually worse scenario, Awaiting next DOMPDF release for patch --}}
+        .page-content td a > img {
+            max-width: 100%;
+        }
+        @endif
     </style>
 @endif
\ No newline at end of file
index 55df43a45a20e0f5474b43691541569b1d4dc957..a951e262de7b368c821f3b3b5c3b4bd0fca095ed 100644 (file)
@@ -4,7 +4,7 @@
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     <title>@yield('title')</title>
 
-    @include('common.export-styles', ['format' => $format])
+    @include('common.export-styles', ['format' => $format, 'engine' => $engine ?? ''])
     @include('common.export-custom-head')
 </head>
 <body>