alifahmmed / pdfbuilder
Laravel 12 package: generate PDF via headless chromium or wkhtmltopdf without Node.js
1.0.0
2025-08-11 11:00 UTC
Requires
- php: >=8.1
- ext-sockets: *
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
README
PdfBuilder is a Laravel package for generating high-quality PDFs from HTML using wkhtmltopdf
or headless Chromium — without requiring Node.js.
It supports Bootstrap, Tailwind CSS, and any modern HTML/CSS styling.
✨ Features
- Convert HTML to PDF directly in Laravel
- No Node.js required
- Supports Bootstrap and TailwindCSS
- Works with
wkhtmltopdf
binary (bundled or system-installed) - Simple Facade API (
PdfBuilder::htmlToPdf()
) - Configurable binary path, temp storage, and extra arguments
- Publish binary to your Laravel project for easy server deployment
- Works on Laravel 9, 10, 11, and 12
📦 Installation
Require the package via Composer:
composer require alifahmmed/pdfbuilder
⚙️ Configuration
- Publish the config file and binary:
php artisan vendor:publish --provider="AlifAhmmed\PdfBuilder\PdfBuilderServiceProvider" --tag=config php artisan vendor:publish --provider="AlifAhmmed\PdfBuilder\PdfBuilderServiceProvider" --tag=bin
- Set up your .env variables:
PDFBUILDER_BINARY_PATH=/full/absolute/path/to/your/project/pdf-builder-bin/wkhtmltopdf PDFBUILDER_RENDERER=wkhtmltopdf
- Configuration file (config/pdfbuilder.php):
return [ 'default_renderer' => 'wkhtmltopdf', 'binary_path' => env('PDFBUILDER_BINARY_PATH', 'wkhtmltopdf'), 'temp_path' => env('PDFBUILDER_TEMP_PATH', storage_path('app/pdfbuilder')), 'extra_args' => [ '--enable-local-file-access', '--no-outline', '--quiet', ], ];
🚀 Basic Usage
Generate a PDF from HTML and return it as a download in a Laravel controller:
use PdfBuilder; class PdfController extends Controller { public function download() { $html = view('pdf.invoice', ['data' => $invoiceData])->render(); $pdfContent = PdfBuilder::htmlToPdf($html); return response($pdfContent) ->header('Content-Type', 'application/pdf') ->header('Content-Disposition', 'attachment; filename="invoice.pdf"'); } }
🎯 Advanced Usage
- Set Custom Page Options
$pdfContent = PdfBuilder::htmlToPdf($html, [ 'orientation' => 'landscape', // 'portrait' or 'landscape' 'margin' => '10mm', ]);
- Inline Local Images Automatically
use AlifAhmmed\PdfBuilder\Helpers; $html = file_get_contents(resource_path('views/pdf/report.html')); $htmlWithImages = Helpers::inlineImages($html, public_path('images')); $pdfContent = PdfBuilder::htmlToPdf($htmlWithImages);
- Store PDF to Disk
Storage::put('reports/monthly.pdf', PdfBuilder::htmlToPdf($html));
🛠 Requirements
- PHP >= 8.1
- Laravel 9.x | 10.x | 11.x | 12.x
- wkhtmltopdf binary (system-installed or bundled via resources/bin)
🐛 Troubleshooting
-
Binary not found Ensure wkhtmltopdf is installed or the .env path is correct.
-
Permission denied Make sure the binary is executable:
chmod +x pdf-builder-bin/wkhtmltopdf
- Fonts/CSS not applied Use absolute paths or the Helpers::inlineImages() method to embed assets.
📜 License
This package is open-sourced under the MIT license.