jbreuer95 / laravel-make-pdf
Convert HTML to PDF using a headless Chrome instance
Requires
- php: ^8.1
- ext-zip: *
- guzzlehttp/guzzle: ^7.9
- illuminate/contracts: ^10.0||^11.0
- php-webdriver/webdriver: ^1.15
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- stevebauman/autodoc-facades: ^1.2
This package is auto-updated.
Last update: 2025-03-03 02:18:25 UTC
README
This package allows you to easily convert HTML to PDF using headless Chrome through Selenium, without needing Node.js. It is inspired by Spatie's laravel-pdf package, which uses BrowserShot and Puppeteer, but our solution offers a more PHP-centric approach using Selenium.
Requirements
Laravel Make PDF requires PHP 8.1+ and Laravel 10+.
Installation & Setup
You can install the package via Composer:
composer require jbreuer95/laravel-make-pdf
After installation, download headless Chrome using the following Artisan command:
php artisan make-pdf:install
To customize the package configuration, publish the configuration file:
php artisan vendor:publish --tag="laravel-make-pdf-config"
Here is the content of the published config file:
return [ // Configuration options will go here ];
Usage
Converting HTML to PDF with this package is simple and efficient. Below are a few common use cases:
Basic Example
Convert a Blade view to a PDF and stream it to the browser:
use Breuer\MakePDF\Facades\PDF; Route::get('/', function () { return PDF::view('view.name', [])->response(); });
Or force the browser to download the PDF file
return PDF::view('view.name', [])->download();
Options
Render Raw HTML:
Instead of passing a Blade view, you can directly pass HTML:
PDF::html('<h1>Hello World</h1>')
Header and Footer
You can include a view in the header and footer of every page:
PDF::view('view.name', []) ->headerView('view.header') ->footerView('view.footer') ->response();
Alternatively, set raw HTML for the header and footer:
->headerHtml('<div>My header</div>') ->footerHtml('<div>My footer</div>')
In the header or footer, the following placeholders can be used and will be replaced with their print-specific values:
<span class="date"></span> <span class="title"></span> <span class="pageNumber"></span> <span class="totalPages"></span>
Note: The header and footer do not inherit the same CSS as the main content, and the default font size is 0. You should include any required CSS directly in the header/footer. Here’s an example of a styled footer view:
<style> footer { font-size: 13px; color: black; } </style> <footer> <span class="date"></span> <span class="pageNumber"></span> / <span class="totalPages"></span> </footer>
Landscape Orientation
Switch the page orientation to landscape:
use Breuer\MakePDF\Enums\Orientation; PDF::landscape()
Set Paper Format
Specify a standard paper format:
use Breuer\MakePDF\Enums\Format; PDF::format(Format::A4)
The following formats are available: LETTER
, LEGAL
, A0
, A1
, A2
, A3
, A4
, A5
, A6
.
Set Custom Paper Size
Set a custom paper size, specifying height and width in inches (or another unit):
use Breuer\MakePDF\Enums\Unit; PDF::paperSize($height, $width) // Uses inches by default PDF::paperSize(29.7, 21, Unit::CENTIMETER) // Uses centimeters and converts to inches
Set Margins
Set custom margins for the PDF document:
use Breuer\MakePDF\Enums\Unit; PDF::margins($top, $right, $bottom, $left) // Uses inches by default PDF::margins(2.54, 1.27, 2.54, 1.27, Unit::CENTIMETER) // Centimeters, converted to inches
Custom Filename
Define a custom name for the PDF when downloading from the browser.
The .pdf
extension is automatically appended if omitted:
PDF::view('view.name', []) ->name('custom_filename') ->response();
Save to File
Use the save
method to store the PDF at a given file path:
->save('/path/to/save/yourfile.pdf')
Retrieve PDF as a String
To obtain the raw PDF content as a string, use the raw
method:
$content = PDF::view('view.name', [])->raw();
Stream PDF
Display the PDF directly in the browser without saving it to disk:
->response()
Force Download
Prompt the browser to immediately download the PDF:
->download()
License
This package is open-sourced software licensed under the MIT License.
Please see the License File for more information.