chrisbraybrooke / laravel-chrome-pdf
A wrapper for Laravel around the spiritix/php-chrome-html2pdf libary
Installs: 12 726
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 4
Open Issues: 5
Requires
- php: >=7.0
- illuminate/filesystem: 5.5.x|5.6.x|5.7.x|5.8.x
- illuminate/support: 5.5.x|5.6.x|5.7.x|5.8.x
- spiritix/php-chrome-html2pdf: 1.3.*
This package is auto-updated.
Last update: 2024-05-06 08:28:15 UTC
README
Important note regarding git
If you keep you vendor folder in source control - then you may want to consider placing the following in you .gitignore
file. The reason for this is that the chromium rendering engine contains files that are larger than GitHubs 100mb limit.
/vendor/spiritix/php-chrome-html2pdf/node_modules/*
How it works
This library is based on puppeteer, a headless Chrome Node API which is maintained by the Chrome DevTools team.
It provides a simple PHP / Laravel wrapper around the Node API, focused on generating beautiful PDF files.
In contrast to other HTML to PDF converters like wkhtmltopdf, the corresponding PHP wrapper or similar libraries, it is based on a current Chrome version instead of outdated and unmaintained WebKit builds. This library therefore fully supports CSS3, HTML5, SVGs, SPAs, and all the other fancy stuff people use these days.
Installation
composer require chrisbraybrooke/laravel-chrome-pdf
Setup:
Laravel >=5.5
Laravel 5.5 and above uses package autodiscovery so you are all done! Skip to Usage.
Laravel 5.5<
If you are using Laravel 5.4 or below, you will have to manually register the package. After updating composer, add the ServiceProvider to the providers array in config/app.php
.
ChrisBraybrooke\LaravelChromePdf\ServiceProvider::class,
And optionally add the Facade.
'ChromePDF' => ChrisBraybrooke\LaravelChromePdf\ChromePDF::class,
Usage:
Below is an example of creating a simple PDF from a blade file.
namespace App\Http\Controllers; use App\Invoice; use ChromePDF; class InvoicesController { /** * Download a PDF version of the invoice. * * @return void */ public function show(Invoice $invoice) { // Load resources/views/invoice.blade.php ChromePDF::loadView('invoice', ['invoice' => $invoice]) ->size('a4') ->landscape() ->download("invoice-{$invoice->ref}.pdf"); } }
Outputs:
There are several methods of outputting the PDF.
Of course download
is available.
ChromePDF::loadView('invoice', ['invoice' => $invoice])->download("invoice-{$ref}.pdf");
Use inline
to show the PDF inline in the browser.
ChromePDF::loadView('invoice', ['invoice' => $invoice])->inline();
Or save
to save the file to the filesystem. The first argument is the filename / path - and the second is the disk to be used.
ChromePDF::loadView('invoice', ['invoice' => $invoice])->save("invoice-{$ref}.pdf", 's3');
Options:
It is simple to set options for the PDF.
Just use the setOption
or setOptions
methods.
ChromePDF::loadHtml('<h1>Hello world</h1>')->setOption('scale', '0.2')->download('hello.pdf');
Or set multiple options at once.
ChromePDF::loadHtml('<h1>More options here!</h1>') ->setOptions(['scale' => 0.2, 'landscape']) ->download('options.pdf');
All available php-chrome-html2pdf options are available.
There are also a few helper methods, which can be chained.
ChromePDF::loadHtml('<h1>Hello world</h1>') // a3 & a5 also available - pass true to set as landscape. Or use size('') and specify a different page size. ->a4() // Set the orientation as landscape - default is portrait. ->landscape() // Load a blade template for the page headers. ->headerView('default-header') // Load a blade template for the page footers. ->footerView('default-footer') ->download();
Maintenance:
My company Purple Mountain - A web development company in the UK, will try our best to keep this package up to date and free from any issues.