Keep the logic for your PDFs in one place like you do with Laravel's Mailables.
You can install the package via composer:
composer require pxlrbt/laravel-pdfable
You can publish the config file with:
php artisan vendor:publish --tag="pdfable-config"
Optionally, you can publish the views using
php artisan vendor:publish --tag="pdfable-views"
Currently two drivers are supported:
- Browsershot (default)
- Wkhtmltopdf (legacy, wkhtmltopdf is deprecated)
This is the driver and requires spatie/browsershot. Please follow the installation instructions for that package.
To use the wkhtmlpdf Driver, make sure wkhtmltopdf
is installed on your system and globally available.
Then, set the PDFABLE_DRIVER
option in your .env
file to wkhtmltopdf
.
You can use the make command to generate a Pdfable class and view.
php artisan make:pdf Invoice
You can directly use, pass or return Pdfables in many places in your app.
You can store Pdfables via ->store()
method. This will use outputFile()
method on the class to determine the class name. Optionally, you can pass a custom filename.
(new Invoice($order)->store()));
You can either stream, download or return your Pdfables HTML for debugging.
To return HTML in a debugging view, just return the Pdfable.
Route::get('/invoice/{order}', fn (Order $order) => new Invoice($order));
To stream your Pdfable, add the ->stream()
method.
Route::get('/invoice/{order}', fn (Order $order) => (new Invoice($order)->stream()));
To download your Pdfable, add the ->download()
method. Optionally, you can also override the filename from here.
Route::get('/invoice/{order}', fn (Order $order) => (new Invoice($order)->download('custom-filename.pdf')));
To use a Pdfable as a mail attachment, just pass it via ->attach()
. Make sure your mailables/notifications are queued for faster processing.
return (new MailMessage)
->subject("Your Invoice")
->attach(new Invoice($order));
Pdfs can take some time to create, so you can queue your Pdfables and create them in the background with the known Laravel methods.
dispatch(new Invoice($order));
// or
Invoice::dispatch($order);
// ...
Once you have generated a pdfable class, open it up so we can explore its contents. Pdfable class configuration is done in several methods.
The view is configured via static $view
property.
class Invoice extends Pdfable
{
public string $view = 'pdf.task';
}
You can return a Page
object to configure the PDF page size, orientation and margins.
public function page(): Page
{
return Page::make()->size(PageSize::A4)->margins('narrow');
}
Pass additional data via the constructor of your Pdfable for later use.
public function __construct(
public Order $order,
public ?Customer $customer = null,
)
{}
Similar to Laravel's Blade Components you can access properties and public methods directly from your view file.
<h1>Invoice for Order {{ $order->id }}</h1>
<div>Total: {{ $getTotal() }}</div>
When saving a Pdfable to the disk, you can provide a default path via filename()
and override the default disk via $disk
property.
public function filename(): string
{
return "customers/{$this->customer->id}/{$this->order->id}.pdf";
}
Pdfables implement ShouldQueue
and therefore can be pushed to a queue via Invoice::dispatch()
. You can also use other queue configuration methods directly on your Pdfable like backoff()
, retryUntil()
, uniqueId()
, ...
The MIT License (MIT). Please see License File for more information.