Skip to content

Commit

Permalink
PDF: Started new command option, merged options, simplified dompdf
Browse files Browse the repository at this point in the history
- Updated DOMPDF to direcly use library instead of depending on barry
wrapper.
- Merged existing export options file into single exports file.
- Defined option for new command option.

Related to #4732
  • Loading branch information
ssddanbrown committed Apr 22, 2024
1 parent d949b97 commit bb6670d
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 150 deletions.
8 changes: 8 additions & 0 deletions .env.example.complete
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ FILE_UPLOAD_SIZE_LIMIT=50
# Can be 'a4' or 'letter'.
EXPORT_PAGE_SIZE=a4

# Export PDF Command
# Set a command which can be used to convert a HTML file into a PDF file.
# When false this will not be used.
# String values represent the command to be called for conversion.
# Supports '{input_html_path}' and '{output_pdf_path}' placeholder values.
# Example: EXPORT_PDF_COMMAND="/scripts/convert.sh {input_html_path} {output_pdf_path}"
EXPORT_PDF_COMMAND=false

# Set path to wkhtmltopdf binary for PDF generation.
# Can be 'false' or a path path like: '/home/bins/wkhtmltopdf'
# When false, BookStack will attempt to find a wkhtmltopdf in the application
Expand Down
1 change: 0 additions & 1 deletion app/Config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
// Application Service Providers
'providers' => ServiceProvider::defaultProviders()->merge([
// Third party service providers
Barryvdh\DomPDF\ServiceProvider::class,
Barryvdh\Snappy\ServiceProvider::class,
SocialiteProviders\Manager\ServiceProvider::class,

Expand Down
53 changes: 38 additions & 15 deletions app/Config/dompdf.php → app/Config/exports.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
<?php

/**
* DOMPDF configuration options.
* Export configuration options.
*
* Changes to these config files are not supported by BookStack and may break upon updates.
* Configuration should be altered via the `.env` file or environment variables.
* Do not edit this file unless you're happy to maintain any changes yourself.
*/

$snappyPaperSizeMap = [
'a4' => 'A4',
'letter' => 'Letter',
];

$dompdfPaperSizeMap = [
'a4' => 'a4',
'letter' => 'letter',
];

$exportPageSize = env('EXPORT_PAGE_SIZE', 'a4');

return [

'show_warnings' => false, // Throw an Exception on warnings from dompdf
// Set a command which can be used to convert a HTML file into a PDF file.
// When false this will not be used.
// String values represent the command to be called for conversion.
// Supports '{input_html_path}' and '{output_pdf_path}' placeholder values.
// Example: EXPORT_PDF_COMMAND="/scripts/convert.sh {input_html_path} {output_pdf_path}"
'pdf_command' => env('EXPORT_PDF_COMMAND', false),

// 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' => [],
],
],

'options' => [
'dompdf' => [
/**
* The location of the DOMPDF font directory.
*
Expand Down Expand Up @@ -101,7 +134,7 @@
/**
* Whether to enable font subsetting or not.
*/
'enable_fontsubsetting' => false,
'enable_font_subsetting' => false,

/**
* The PDF rendering backend to use.
Expand Down Expand Up @@ -165,7 +198,7 @@
*
* @see CPDF_Adapter::PAPER_SIZES for valid sizes ('letter', 'legal', 'A4', etc.)
*/
'default_paper_size' => $dompdfPaperSizeMap[env('EXPORT_PAGE_SIZE', 'a4')] ?? 'a4',
'default_paper_size' => $dompdfPaperSizeMap[$exportPageSize] ?? 'a4',

/**
* The default paper orientation.
Expand Down Expand Up @@ -268,15 +301,6 @@
*/
'font_height_ratio' => 1.1,

/**
* Enable CSS float.
*
* Allows people to disabled CSS float support
*
* @var bool
*/
'enable_css_float' => true,

/**
* Use the HTML5 Lib parser.
*
Expand All @@ -286,5 +310,4 @@
*/
'enable_html5_parser' => true,
],

];
34 changes: 0 additions & 34 deletions app/Config/snappy.php

This file was deleted.

56 changes: 49 additions & 7 deletions app/Entities/Tools/PdfGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@

namespace BookStack\Entities\Tools;

use Barryvdh\DomPDF\Facade\Pdf as DomPDF;
use Barryvdh\Snappy\Facades\SnappyPdf;
use Dompdf\Dompdf;

class PdfGenerator
{
const ENGINE_DOMPDF = 'dompdf';
const ENGINE_WKHTML = 'wkhtml';
const ENGINE_COMMAND = 'command';

/**
* Generate PDF content from the given HTML content.
*/
public function fromHtml(string $html): string
{
if ($this->getActiveEngine() === self::ENGINE_WKHTML) {
$engine = $this->getActiveEngine();

if ($engine === self::ENGINE_WKHTML) {
$pdf = SnappyPDF::loadHTML($html);
$pdf->setOption('print-media-type', true);
} else {
$pdf = DomPDF::loadHTML($html);
return $pdf->output();
} else if ($engine === self::ENGINE_COMMAND) {
// TODO - Support PDF command
return '';
}

return $pdf->output();
return $this->renderUsingDomPdf($html);
}

/**
Expand All @@ -31,8 +36,45 @@ public function fromHtml(string $html): string
*/
public function getActiveEngine(): string
{
$useWKHTML = config('snappy.pdf.binary') !== false && config('app.allow_untrusted_server_fetching') === true;
$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) {
return self::ENGINE_WKHTML;
}

return self::ENGINE_DOMPDF;
}

protected function renderUsingDomPdf(string $html): string
{
$options = config('exports.dompdf');
$domPdf = new Dompdf($options);
$domPdf->setBasePath(base_path('public'));

return $useWKHTML ? self::ENGINE_WKHTML : self::ENGINE_DOMPDF;
$domPdf->loadHTML($this->convertEntities($html));
$domPdf->render();

return (string) $domPdf->output();
}

/**
* Taken from https://github.com/barryvdh/laravel-dompdf/blob/v2.1.1/src/PDF.php
* Copyright (c) 2021 barryvdh, MIT License
* https://github.com/barryvdh/laravel-dompdf/blob/v2.1.1/LICENSE
*/
protected function convertEntities(string $subject): string
{
$entities = [
'€' => '&euro;',
'£' => '&pound;',
];

foreach ($entities as $search => $replace) {
$subject = str_replace($search, $replace, $subject);
}
return $subject;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"ext-mbstring": "*",
"ext-xml": "*",
"bacon/bacon-qr-code": "^2.0",
"barryvdh/laravel-dompdf": "^2.0",
"barryvdh/laravel-snappy": "^1.0",
"doctrine/dbal": "^3.5",
"dompdf/dompdf": "^2.0",
"guzzlehttp/guzzle": "^7.4",
"intervention/image": "^3.5",
"laravel/framework": "^10.10",
Expand Down

0 comments on commit bb6670d

Please sign in to comment.