tenthfeet / laravel-pdf
A portable PDF adapter for Laravel supporting multiple drivers.
Requires
- php: ^8.2
- illuminate/http: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.0|^11.0
Suggests
- dompdf/dompdf: Required to use the Dompdf driver.
- tecnickcom/tcpdf: Required to use the TCPDF driver.
README
A flexible, driver-based PDF generation package for Laravel. This package allows you to decouple your PDF templates from specific libraries (like TCPDF or Dompdf), making your application more maintainable and ready for future engine swaps (e.g., adding Indic font support).
Features
- Driver-Based Architecture: Switch between engines (TCPDF, Dompdf, etc.) without changing your template logic.
- Unified API: Clean, Laravel-style Facade for generating documents.
- Template Inheritance: Simplified base classes for building complex PDF reports.
- Hook System: Integrated support for Headers, Footers, and Watermarks via simple interfaces.
- Fluent API: Easily stream or download files.
Requirements
- PHP:
^8.2 - Laravel:
^10.0|^11.0|^12.0|^13.0
Installation
You can install the package via composer:
composer require tenthfeet/laravel-pdf
Installation
You can install the package via composer:
composer require tenthfeet/laravel-pdf
Configuration
Publish the configuration file:
php artisan vendor:publish --tag=pdf-config
In config/pdf.php, you can set the default driver and configure individual engine settings:
return [ 'default' => env('PDF_DRIVER', 'tcpdf'), 'drivers' => [ 'tcpdf' => [ 'class' => \Tenthfeet\Pdf\Adapters\TcpdfAdapter::class, ], // ... ], ];
Usage
1. Create a Template
Extend the PdfDocument class and implement the body method.
namespace App\Pdf\Templates; use Tenthfeet\Pdf\PdfDocument; use Tenthfeet\Pdf\Contracts\PdfAdapter; class MyReport extends PdfDocument { public function body(PdfAdapter $pdf): void { $pdf->writeHTML("<p>This is the document body.</p>"); } }
2. Hooks & Interfaces
You can add headers, footers, or watermarks to your templates by implementing the corresponding interfaces.
Headers & Footers
Implement HasHeader or HasFooter to inject content into every page.
use Tenthfeet\Pdf\Contracts\HasHeader; use Tenthfeet\Pdf\Contracts\HasFooter; class MyReport extends PdfDocument implements HasHeader, HasFooter { public function header(PdfAdapter $pdf): void { $pdf->writeHTML("<h1>Page Header</h1>"); } public function footer(PdfAdapter $pdf): void { $pdf->SetY(-15); $pdf->writeHTML("<p>Page Footnote</p>"); } public function body(PdfAdapter $pdf): void { // ... } }
Watermarks
Implement HasWatermark to add a background watermark.
use Tenthfeet\Pdf\Contracts\HasWatermark; class MyReport extends PdfDocument implements HasWatermark { public function watermark(PdfAdapter $pdf, string $text = 'DRAFT'): void { // You can use the built-in helper from the base class parent::watermark($pdf, $text); } }
3. Generate the PDF
Using the Pdf facade:
use Tenthfeet\Pdf\Facades\Pdf; use App\Pdf\Templates\InvoiceReport; public function download(Request $request) { $document = new InvoiceReport($request->all()); return Pdf::make($document)->download('invoice.pdf'); }
3. Swapping Drivers
You can override the driver globally in your .env or per-template by adding a $driver property:
class SpecialReport extends PdfDocument { public ?string $driver = 'dompdf'; // Force this template to use Dompdf }
License
The MIT License (MIT). Please see License File for more information.