From dcd6574b2d5ced2c82ff60369e6e88523ee2b914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 14 Dec 2020 17:23:04 +0100 Subject: [PATCH] Experimental PDF rendering: - Added experimental flag in ninja.php - Added experimental rendering in PdfMaker.php - Added dynamic $global-margin for 1cm/0cm based on PDF method --- app/Utils/HtmlEngine.php | 7 ++-- app/Utils/Traits/Pdf/PdfMaker.php | 10 +++++ composer.json | 1 + composer.lock | 47 ++++++++++++++++++++++- config/ninja.php | 4 +- resources/views/pdf-designs/bold.html | 1 + resources/views/pdf-designs/business.html | 2 +- resources/views/pdf-designs/clean.html | 2 +- resources/views/pdf-designs/creative.html | 2 +- resources/views/pdf-designs/elegant.html | 2 +- resources/views/pdf-designs/hipster.html | 2 +- resources/views/pdf-designs/plain.html | 6 +-- resources/views/pdf-designs/playful.html | 2 +- 13 files changed, 73 insertions(+), 15 deletions(-) diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 3841cda5f5..e86e503dee 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -92,8 +92,7 @@ class HtmlEngine } $data = []; - $data['$global-margin'] = ['value' => 'm-8', 'label' => '']; - $data['$global-padding'] = ['value' => 'p-8', 'label' => '']; + $data['$global-margin'] = ['value' => config('ninja.experimental_pdf_engine') ? '0cm' : '1cm', 'label' => '']; $data['$tax'] = ['value' => '', 'label' => ctrans('texts.tax')]; $data['$app_url'] = ['value' => $this->generateAppUrl(), 'label' => '']; $data['$from'] = ['value' => '', 'label' => ctrans('texts.from')]; @@ -155,7 +154,7 @@ class HtmlEngine } else { $data['$balance_due'] = ['value' => Number::formatMoney($this->entity->balance, $this->client) ?: ' ', 'label' => ctrans('texts.balance_due')]; } - + $data['$quote.balance_due'] = $data['$balance_due']; $data['$invoice.balance_due'] = $data['$balance_due']; $data['$balance_due'] = $data['$balance_due']; @@ -243,7 +242,7 @@ class HtmlEngine $data['$client.postal_city_state'] = &$data['$postal_city_state']; $data['$client.country'] = &$data['$country']; $data['$client.email'] = &$data['$email']; - + $data['$client.currency'] = ['value' => $this->client->currency()->code, 'label' => '']; $data['$client.balance'] = ['value' => Number::formatMoney($this->client->balance, $this->client), 'label' => ctrans('texts.account_balance')]; diff --git a/app/Utils/Traits/Pdf/PdfMaker.php b/app/Utils/Traits/Pdf/PdfMaker.php index 25fd6ef594..d7c23960e7 100644 --- a/app/Utils/Traits/Pdf/PdfMaker.php +++ b/app/Utils/Traits/Pdf/PdfMaker.php @@ -11,6 +11,7 @@ namespace App\Utils\Traits\Pdf; +use Beganovich\ChromiumPdf\ChromiumPdf; use Spatie\Browsershot\Browsershot; trait PdfMaker @@ -26,6 +27,15 @@ trait PdfMaker */ public function makePdf($header, $footer, $html) { + if (config('ninja.experimental_pdf_engine')) { + $pdf = new ChromiumPdf(); + + return $pdf + ->setChromiumPath(config('ninja.experimental_pdf_engine_chromium_path')) + ->setHtml($html) + ->generate(); + } + $browser = Browsershot::html($html); if (config('ninja.system.node_path')) { diff --git a/composer.json b/composer.json index 852cc0ceb6..fdcd1945ef 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "ext-json": "*", "asgrim/ofxparser": "^1.2", "authorizenet/authorizenet": "^2.0", + "beganovich/chromium-pdf": "dev-master", "checkout/checkout-sdk-php": "^1.0", "cleverit/ubl_invoice": "^1.3", "composer/composer": "^2", diff --git a/composer.lock b/composer.lock index 578bffb047..f8f336f200 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": "35318cb6b03b84487f53dc09f2262030", + "content-hash": "75cb7287f45830678381d8e27486cf4a", "packages": [ { "name": "asgrim/ofxparser", @@ -204,6 +204,50 @@ }, "time": "2020-12-11T19:12:18+00:00" }, + { + "name": "beganovich/chromium-pdf", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/beganovich/chromium-pdf.git", + "reference": "0f1641f0f8272b68aa4942307b85fba5596a90c3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/beganovich/chromium-pdf/zipball/0f1641f0f8272b68aa4942307b85fba5596a90c3", + "reference": "0f1641f0f8272b68aa4942307b85fba5596a90c3", + "shasum": "" + }, + "require": { + "php": "^7.3|^7.4|^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.5" + }, + "default-branch": true, + "type": "library", + "autoload": { + "psr-4": { + "Beganovich\\ChromiumPdf\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Benjamin Beganović", + "email": "k1pstabug@gmail.com" + } + ], + "description": "Generate PDFs using headless Chromium", + "support": { + "issues": "https://github.com/beganovich/chromium-pdf/issues", + "source": "https://github.com/beganovich/chromium-pdf/tree/master" + }, + "time": "2020-12-14T15:49:17+00:00" + }, { "name": "brick/math", "version": "0.9.1", @@ -13836,6 +13880,7 @@ "aliases": [], "minimum-stability": "dev", "stability-flags": { + "beganovich/chromium-pdf": 20, "webpatser/laravel-countries": 20 }, "prefer-stable": true, diff --git a/config/ninja.php b/config/ninja.php index af59f5edb0..6119bfb336 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -32,7 +32,7 @@ return [ 'phantomjs_secret' => env('PHANTOMJS_SECRET', false), 'phantomjs_pdf_generation' => env('PHANTOMJS_PDF_GENERATION', true), 'trusted_proxies' => env('TRUSTED_PROXIES', false), - + 'sentry_dsn' => env('SENTRY_LARAVEL_DSN', 'https://9b4e15e575214354a7d666489783904a@sentry.invoicing.co/6'), 'environment' => env('NINJA_ENVIRONMENT', 'selfhost'), // 'hosted', 'development', 'selfhost', 'reseller' @@ -135,4 +135,6 @@ return [ 'designs' => [ 'base_path' => resource_path('views/pdf-designs/'), ], + 'experimental_pdf_engine' => env('EXPERIMENTAL_PDF_ENGINE', false), + 'experimental_pdf_engine_chromium_path' => env('EXPERIMENTAL_PDF_ENGINE_CHROMIUM_PATH', null), ]; diff --git a/resources/views/pdf-designs/bold.html b/resources/views/pdf-designs/bold.html index 4090a44c16..a3c4fd1784 100644 --- a/resources/views/pdf-designs/bold.html +++ b/resources/views/pdf-designs/bold.html @@ -14,6 +14,7 @@ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-family: Arial, Helvetica, sans-serif; + margin: "$global-margin"; } p { diff --git a/resources/views/pdf-designs/business.html b/resources/views/pdf-designs/business.html index ff23b04d46..f29891db96 100644 --- a/resources/views/pdf-designs/business.html +++ b/resources/views/pdf-designs/business.html @@ -12,7 +12,7 @@ } @page { - margin: 1cm; + margin: "$global-margin"; } p { diff --git a/resources/views/pdf-designs/clean.html b/resources/views/pdf-designs/clean.html index 9bf61d599a..28cfeabcaf 100644 --- a/resources/views/pdf-designs/clean.html +++ b/resources/views/pdf-designs/clean.html @@ -9,7 +9,7 @@ -moz-osx-font-smoothing: grayscale; font-family: Arial, Helvetica, sans-serif; font-size: "$font_size"; - margin: 1cm; + margin: "$global-margin"; } p { diff --git a/resources/views/pdf-designs/creative.html b/resources/views/pdf-designs/creative.html index 03e3489dcf..5aa384ccae 100644 --- a/resources/views/pdf-designs/creative.html +++ b/resources/views/pdf-designs/creative.html @@ -30,7 +30,7 @@ } body { - margin: 2rem; + margin: "$global-margin"; } .header-wrapper { diff --git a/resources/views/pdf-designs/elegant.html b/resources/views/pdf-designs/elegant.html index 35342cfa65..7686f5f225 100644 --- a/resources/views/pdf-designs/elegant.html +++ b/resources/views/pdf-designs/elegant.html @@ -17,7 +17,7 @@ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-family: Arial, Helvetica, sans-serif; - margin: 1cm; + margin: "$global-margin"; } .company-logo-wrapper { diff --git a/resources/views/pdf-designs/hipster.html b/resources/views/pdf-designs/hipster.html index 8f48284818..42b6322bff 100644 --- a/resources/views/pdf-designs/hipster.html +++ b/resources/views/pdf-designs/hipster.html @@ -20,7 +20,7 @@ } @page { - margin: 1cm; + margin: "$global-margin"; } .header-wrapper { diff --git a/resources/views/pdf-designs/plain.html b/resources/views/pdf-designs/plain.html index 6384395e8d..c72398f9fc 100644 --- a/resources/views/pdf-designs/plain.html +++ b/resources/views/pdf-designs/plain.html @@ -13,7 +13,7 @@ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-family: Arial, Helvetica, sans-serif; - margin: 1cm; + margin: "$global-margin"; } .header-wrapper { @@ -58,8 +58,8 @@ flex-direction: column; } - #product-table, - #delivery-note-table, + #product-table, + #delivery-note-table, #task-table { min-width: 100%; table-layout: fixed; diff --git a/resources/views/pdf-designs/playful.html b/resources/views/pdf-designs/playful.html index 95c9f3d2ac..96516134d9 100644 --- a/resources/views/pdf-designs/playful.html +++ b/resources/views/pdf-designs/playful.html @@ -17,7 +17,7 @@ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-family: Arial, Helvetica, sans-serif; - margin: 1cm; + margin: "$global-margin"; } .header-wrapper {