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.
Laravel Make PDF requires PHP 8.1+ and Laravel 10+.
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
];
Converting HTML to PDF with this package is simple and efficient. Below are a few common use cases:
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();
Instead of passing a Blade view, you can directly pass HTML:
PDF::html('<h1>Hello World</h1>')
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>
Switch the page orientation to landscape:
use Breuer\MakePDF\Enums\Orientation;
PDF::landscape()
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 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 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
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();
Use the save
method to store the PDF at a given file path:
->save('/path/to/save/yourfile.pdf')
To obtain the raw PDF content as a string, use the raw
method:
$content = PDF::view('view.name', [])->raw();
Display the PDF directly in the browser without saving it to disk:
->response()
Prompt the browser to immediately download the PDF:
->download()
This package is open-sourced software licensed under the MIT License.
Please see the License File for more information.