trianity / laravel-mpdf
Laravel integration for generating PDF documents with mPDF.
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/support: ^12.0|^13.0
- mpdf/mpdf: ^8.3
Requires (Dev)
- larastan/larastan: ^2.0|^3.0
- laravel/pint: ^1.2|^2.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0|^5.0
This package is auto-updated.
Last update: 2026-08-29 00:06:33 UTC
README
Laravel integration for mPDF, maintained in the Trianity GitHub repository. It provides a small wrapper for generating PDF files from HTML, files, and Laravel views.
Requirements
- PHP 8.4 or newer
- Laravel 12 or 13 (the package uses
illuminate/support) - mPDF 8.3.x
Installation
composer require trianity/laravel-mpdf
The service provider and PDF facade are registered automatically by Laravel package discovery.
Dependency injection is also supported:
use Trianity\LaravelMpdf\LaravelMpdfWrapper; public function show(LaravelMpdfWrapper $pdf) { return $pdf->loadHTML('<h1>Hello</h1>')->output(); }
Usage
use Trianity\LaravelMpdf\Facades\PDF; class ReportController { public function show() { $pdf = PDF::loadView('pdf.document', ['name' => 'World']); $pdf->stream('document.pdf'); } }
The wrapper supports the following methods:
PDF::loadHTML('<h1>Hello</h1>'); PDF::loadFile(storage_path('app/document.html')); PDF::loadView('pdf.document', $data); PDF::chunkLoadHTML($html, '<html-separator/>'); PDF::chunkLoadFile($file, '<html-separator/>'); PDF::chunkLoadView('pdf.document', '<html-separator/>', $data);
Chunk methods split the rendered HTML at the supplied separator and write each part separately. This can help with large HTML documents and PCRE backtrack limits. The separator must not be an empty string.
The returned LaravelMpdf instance provides:
$pdf->output(); // Return the PDF as a string $pdf->save($path); // Save the PDF and return $path $pdf->download('document.pdf'); // Send it as a download $pdf->stream('document.pdf'); // Send it inline $pdf->getMpdf()->AddPage(); // Access the underlying mPDF instance $pdf->WriteHTML('<p>More HTML</p>');
output() returns the PDF contents. save() returns the supplied path. download() and stream() send the PDF directly through mPDF and return nothing.
Configuration
Publish the configuration file with:
php artisan vendor:publish --tag=mpdf-config
The published file is config/pdf.php. It contains the mPDF defaults used by the wrapper, including page format, margins, fonts, language detection, watermarks, PDF/A, and temporary directory settings.
Configuration can also be overridden for an individual document. The per-document values take precedence over the application configuration:
$pdf = PDF::loadView('pdf.document', $data, [], [ 'format' => 'A5', 'orientation' => 'L', 'title' => 'Another title', ]);
Custom fonts
Set a font directory and font data in config/pdf.php:
return [ 'custom_font_dir' => base_path('resources/fonts'), 'custom_font_data' => [ 'examplefont' => [ 'R' => 'ExampleFont-Regular.ttf', 'B' => 'ExampleFont-Bold.ttf', 'I' => 'ExampleFont-Italic.ttf', 'BI' => 'ExampleFont-Bold-Italic.ttf', ], ], ];
Use the font in your HTML/CSS as font-family: examplefont;.
Headers and footers
mPDF headers and footers can be defined in the HTML and CSS passed to the wrapper:
<htmlpageheader name="page-header">Your header</htmlpageheader> <htmlpagefooter name="page-footer">Page {PAGENO}</htmlpagefooter>
@page { header: page-header; footer: page-footer; }
See the mPDF documentation for the complete list of supported HTML, CSS, and configuration options.
Testing
Install the development dependencies and run the complete local check with:
composer install composer check
The check runs Pest, Larastan/PHPStan, and Pint.
Attribution
The package has an earlier history in the Trianity GitHub repository. The previous clone was abandoned and the current codebase was rewritten by Trianity while preserving the supported public API and configuration behavior.
The source still contains the original Carlos Meneses author attribution. The current package namespace and maintenance are provided by Trianity.
Notes
mPDF may block while fetching external HTTP resources when the application runs on a single-threaded server such as php -S or artisan serve. Use a production web server for applications that need external resources.
License
This package is open-sourced software licensed under the MIT license.