safermobility / laravel-gotenberg
Create PDFs in Laravel apps using Gotenberg
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
- illuminate/http: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
This package provides a simple way to create PDFs in Laravel apps. Under the hood it uses Gotenberg to generate PDFs from Blade views. You can use modern CSS features like grid and flexbox to create beautiful PDFs.
This package is very heavily based on Spatie's laravel-pdf package, but does not require Node.js to run, making it more suitable for applications that run in containers.
Installation
You can install the package via composer:
composer require safermobility/laravel-gotenberg
You can publish the config file with:
php artisan vendor:publish --tag="laravel-gotenberg-config"
This is the contents of the published config file:
return [ 'host' => env('GOTENBERG_HOST'), ];
Usage
You must have a working Gotenberg instance.
For development, you may be able to use the Gotenberg Demo server:
GOTENBERG_HOST=https://demo.gotenberg.dev
For production, you must set up your own Gotenberg instance.
Simple PDF generation
use SaferMobility\LaravelGotenberg\Facades\Gotenberg; Gotenberg::view('pdfs.invoice', ['invoice' => $invoice]) ->format('letter') ->save('invoice.pdf')
This will render the Blade view pdfs.invoice
with the given data and save it as a PDF file.
You can also return the PDF as a response from your controller:
use SaferMobility\LaravelGotenberg\Facades\Gotenberg; class DownloadInvoiceController { public function __invoke(Invoice $invoice) { return Gotenberg::view('pdfs.invoice', ['invoice' => $invoice]) ->format('letter') ->name('your-invoice.pdf'); } }
This will use a streamed response to reduce memory usage.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- SaferMobility
- Spatie - for their Laravel PDF package, and many others
License
The MIT License (MIT). Please see License File for more information.