Simple Laravel (^12.0) wrapper for Mpdf, using Laravel's view components.
composer require sglms/pdf
Laravel's auto-discovery features will register the service provider and facade.
For the impatient:
use Sglms\Pdf;
$pdf = Pdf::view('pdf.filename')->output('filename.pdf');
use Sglms\Pdf;
$pdf = Pdf::view(
'pdf.filename',
['param' => 'value'] /* [Optional */
);
$pdf->output('filename.pdf');
/* Or, ... */
$pdf->save('filename');
/* Or, store it 'the Laravel way' */
$pdf->storeAs('filename', 'disk');
$pdf = Pdf::init(
config: ['format' => 'letter'],
header: 'pdf.header',
footer: 'pdf.footer'
stylesheet: 'path/to/stylesheet.css'
);
Add your logo (available as 'var:logo' in your views):
$pdf->logo('path/to/logo.svg');
Remember to add your logo before header/footer setup, if you plan to use it there.
You can override the setup if you need parameters:
$pdf->header('pdf.header', ['param' => 'value']);
$pdf->footer('pdf.footer', ['param' => 'value']);
Include your header before adding views to the pdf. This is a limitation of mPDF in that it calls AddPage()
when you include a view (or any other html), and if the header is not set, mpdf will render the header blank.
Concatenate multiple views:
$pdf = Pdf::init();
$pdf->view('pdf.one')->view('pdf.two')->output('filename.pdf');
Sign your document:
$pdf->sign('pdf.signature', ['name' => 'John Doe']);
Or, ...
$pdf->sign(
signature: 'pdf.signature',
data: ['name' => 'John Doe'],
x: 50, /* [mm] */
y: 100, /* [mm] */
width: 100 /* [mm] */
);
You can also load and sign an existing PDF file:
$pdf->signFile('pdf.signature, 'path/to/pdf');
If you need the base mPDF to work on it further:
$pdf->get();
use Sglms\Pdf;
class CustomController extendes Controller
{
// ...
public function render() {
$pdf = Pdf::view('pdf.filename')->sign('pdf.signature');
return response($pdf->output(), 200)
->header('Content-Type', 'application/pdf')
->header('Content-Disposition', 'inline; filename="document.pdf"');
}
//...
}
As mentioned, very simple (but efficient) wrapper, that works with Laravel 12 (php >=8.3; mpdf >= 8.x).
It's better suited for single page document generation.
This package is part of a larger project in which we needed to generate a large number of one-page documents and (digitally) sign them.
Sglms/Pdf is licensed under the The MIT License.