deecodek / larapdfx
Modern, production-ready PDF generation for Laravel with full CSS3 support, perfect rendering, and advanced features.
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
pkg:composer/deecodek/larapdfx
Requires
- php: ^8.2|^8.3|^8.4
- illuminate/contracts: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
- spatie/browsershot: ^4.0|^5.0
Requires (Dev)
- laravel/pint: ^1.26
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0|^11.0
This package is auto-updated.
Last update: 2025-11-30 07:09:13 UTC
README
Modern, production-ready PDF generation for Laravel with full CSS3 support, perfect rendering, and advanced features.
LaraPDFX solves all the pain points of existing PDF libraries by using Chrome headless rendering engine, giving you pixel-perfect PDFs with modern CSS support including Flexbox, Grid, and all CSS3 features.
๐ฏ Why LaraPDFX?
Problems with Existing Solutions (DomPDF, TCPDF, etc.):
โ Only CSS 2.1 support - no modern CSS
โ Slow performance & memory issues
โ Poor font rendering & RTL language support
โ Image handling problems
โ No Flexbox or Grid support
โ Bootstrap/Tailwind CSS incompatible
โ Complex table layouts break
โจ LaraPDFX Advantages:
โ
Full CSS3 support - Flexbox, Grid, modern properties
โ
Perfect rendering - Chrome engine ensures pixel-perfect output
โ
Fast & efficient - Optimized memory usage
โ
Modern frameworks - Works with Bootstrap, Tailwind, any CSS
โ
RTL languages - Perfect Arabic, Hebrew, Persian support
โ
Custom fonts - All web fonts work flawlessly
โ
Advanced features - Encryption, watermarks, metadata
โ
Production ready - Tested and battle-proven
๐ Requirements
- PHP 8.1 or higher
- Laravel 10.x or 11.x
- Chrome/Chromium installed on your system
๐ Installation
Step 1: Install via Composer
composer require deecodek/larapdfx
Step 2: Install Chrome/Chromium
Ubuntu/Debian:
sudo apt update sudo apt install chromium-browser
Alpine Linux (Docker):
apk add --no-cache chromium
macOS:
brew install --cask google-chrome
Windows: Download from https://www.google.com/chrome/
Step 3: Publish Configuration
php artisan larapdfx:install
This will:
- Publish configuration file to
config/larapdfx.php - Check for Chrome installation
- Guide you through setup
Step 4: Test Installation
php artisan larapdfx:test
This generates a test PDF at storage/app/test.pdf to verify everything works.
๐ Usage
Basic Usage
use Deecodek\LaraPDFX\Facades\PDF; // Generate from Blade view return PDF::view('invoice', ['data' => $data]) ->download('invoice.pdf'); // Generate from HTML string $html = '<h1>Hello World</h1>'; return PDF::html($html) ->stream('document.pdf'); // Generate from URL return PDF::url('https://example.com') ->download('page.pdf');
Save to File
PDF::view('report', $data) ->save(storage_path('app/reports/report.pdf')); // Or use storage disk PDF::view('invoice', $data) ->save(storage_path('app/public/invoice.pdf'));
Download PDF
// Direct download return PDF::view('invoice', $data) ->download('invoice.pdf');
Stream PDF (Display in Browser)
// Display inline in browser return PDF::view('document', $data) ->stream('document.pdf');
Base64 Encoding
$base64 = PDF::view('report', $data)->base64(); // Use in email or API response return response()->json([ 'pdf' => $base64 ]);
๐จ Page Settings
Paper Size
// Predefined formats PDF::view('invoice', $data) ->format('A4') // A4, A3, A5, Letter, Legal, Tabloid, Ledger ->download(); // Or use shortcuts PDF::view('invoice', $data)->a4()->download(); PDF::view('invoice', $data)->letter()->download(); PDF::view('invoice', $data)->legal()->download(); // Custom size (in millimeters) PDF::view('invoice', $data) ->paperSize(210, 297) // Custom width x height ->download();
Orientation
// Landscape PDF::view('report', $data) ->landscape() ->download(); // Portrait (default) PDF::view('report', $data) ->portrait() ->download(); // Or use string PDF::view('report', $data) ->orientation('landscape') ->download();
Margins
// All sides (in millimeters) PDF::view('document', $data) ->margins(20) // 20mm all sides ->download(); // Top/Right/Bottom/Left PDF::view('document', $data) ->margins(10, 15, 10, 15) ->download(); // Using array PDF::view('document', $data) ->margins([ 'top' => 10, 'right' => 15, 'bottom' => 10, 'left' => 15, ]) ->download();
Scale
// Scale the rendering (0.1 to 2.0) PDF::view('document', $data) ->scale(0.8) // 80% scale ->download();
๐ Headers & Footers
Simple Footer with Page Numbers
PDF::view('report', $data) ->footerWithPageNumbers() ->download();
Custom Header
$header = '<div style="text-align:center; padding:10px;">Company Name</div>'; PDF::view('invoice', $data) ->header($header) ->download();
Custom Footer
$footer = '<div style="text-align:center; font-size:10px;"> Page <span class="pageNumber"></span> of <span class="totalPages"></span> </div>'; PDF::view('report', $data) ->footer($footer) ->download();
Available Placeholders in Headers/Footers
<span class="pageNumber"></span>- Current page number<span class="totalPages"></span>- Total pages<span class="date"></span>- Current date<span class="title"></span>- Document title<span class="url"></span>- Document URL
๐ Security Features
Password Protection
// User password (required to open PDF) PDF::view('confidential', $data) ->password('secret123') ->download(); // Owner password (required to modify) PDF::view('document', $data) ->ownerPassword('admin123') ->download();
Permissions
PDF::view('document', $data) ->allowPrinting(false) // Disable printing ->allowCopy(false) // Disable copying ->allowModify(false) // Disable modifications ->download();
๐ง Watermarks
Text Watermark
PDF::view('document', $data) ->watermark('CONFIDENTIAL') ->download(); // Custom watermark options PDF::view('document', $data) ->watermark('DRAFT', [ 'opacity' => 0.5, 'fontSize' => '60px', 'color' => '#ff0000', 'rotation' => -45, ]) ->download();
๐ Metadata
Document Properties
PDF::view('report', $data) ->title('Annual Report 2025') ->author('John Doe') ->subject('Financial Report') ->keywords(['finance', 'annual', 'report']) ->creator('LaraPDFX') ->download(); // Or set all at once PDF::view('document', $data) ->metadata([ 'title' => 'My Document', 'author' => 'Jane Smith', 'subject' => 'Important Report', 'keywords' => 'business, report', 'creator' => 'My Application', ]) ->download();
๐จ CSS Support
LaraPDFX supports ALL modern CSS features because it uses Chrome's rendering engine:
โ Fully Supported
- CSS3 - All properties
- Flexbox - Complete support
- Grid - Full grid layouts
- Custom fonts - Web fonts, Google Fonts
- RTL languages - Arabic, Hebrew, Persian
- Bootstrap - All versions
- Tailwind CSS - Complete support
- Animations - CSS animations/transitions
- Media queries - Print media queries
- Modern selectors - :nth-child, :not, etc.
- Background images - All formats
- SVG - Inline and external
- Box shadows - All shadow effects
- Border radius - Rounded corners
- Gradients - Linear and radial
- Transforms - Rotate, scale, etc.
Example with Modern CSS
{{-- resources/views/pdf/modern-invoice.blade.php --}} <!DOCTYPE html> <html> <head> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2/dist/tailwind.min.css" rel="stylesheet"> </head> <body> <div class="container mx-auto p-8"> <div class="grid grid-cols-2 gap-4"> <div class="bg-blue-100 p-4 rounded-lg shadow-md"> <h2 class="text-2xl font-bold">Invoice</h2> </div> <div class="bg-gray-100 p-4 rounded-lg"> <p class="text-right">Date: {{ $date }}</p> </div> </div> </div> </body> </html>
PDF::view('pdf.modern-invoice', $data) ->download('invoice.pdf');
๐ RTL Language Support
Perfect support for Right-to-Left languages:
<!DOCTYPE html> <html dir="rtl" lang="ar"> <head> <meta charset="UTF-8"> <style> body { font-family: 'Arial', sans-serif; direction: rtl; text-align: right; } </style> </head> <body> <h1>ู ุฑุญุจุง ุจู</h1> <p>ูุฐุง ูุต ุชุฌุฑูุจู ุจุงููุบุฉ ุงูุนุฑุจูุฉ</p> </body> </html>
โ๏ธ Configuration
Edit config/larapdfx.php:
return [ // Default paper format 'paper' => [ 'format' => 'A4', 'margins' => [ 'top' => 10, 'right' => 10, 'bottom' => 10, 'left' => 10, ], ], // Chrome path (auto-detect if null) 'chrome_path' => null, // Node.js paths 'node_binary' => null, 'npm_binary' => null, // Timeout in seconds 'timeout' => 60, // Print background graphics 'print_background' => true, // Default output directory 'output_directory' => 'pdfs', // Queue settings 'queue' => [ 'enabled' => false, 'connection' => null, 'queue' => 'default', ], ];
๐ง Advanced Usage
Custom Chrome Path
PDF::view('document', $data) ->setChromePath('/usr/bin/chromium-browser') ->download();
Custom Timeout
PDF::view('large-report', $data) ->timeout(120) // 2 minutes ->download();
Print Background
PDF::view('colorful-doc', $data) ->printBackground(true) // Print background colors/images ->download();
Prefer CSS Page Size
PDF::view('custom-layout', $data) ->preferCSSPageSize(true) // Use CSS @page size ->download();
๐ฏ Real-World Examples
Invoice with Logo and Styling
{{-- resources/views/invoices/template.blade.php --}} <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> body { font-family: Arial, sans-serif; } .header { background: #3490dc; color: white; padding: 20px; } .invoice-table { width: 100%; border-collapse: collapse; } .invoice-table th, .invoice-table td { border: 1px solid #ddd; padding: 8px; } </style> </head> <body> <div class="header"> <h1>INVOICE #{{ $invoice->number }}</h1> </div> <table class="invoice-table"> <thead> <tr> <th>Item</th> <th>Quantity</th> <th>Price</th> <th>Total</th> </tr> </thead> <tbody> @foreach($invoice->items as $item) <tr> <td>{{ $item->name }}</td> <td>{{ $item->qty }}</td> <td>${{ $item->price }}</td> <td>${{ $item->total }}</td> </tr> @endforeach </tbody> </table> </body> </html>
// Controller public function downloadInvoice($id) { $invoice = Invoice::with('items')->findOrFail($id); return PDF::view('invoices.template', ['invoice' => $invoice]) ->format('A4') ->margins(15) ->footerWithPageNumbers() ->title('Invoice #' . $invoice->number) ->author('Your Company') ->download('invoice-' . $invoice->number . '.pdf'); }
Report with Charts (using Chart.js)
<!DOCTYPE html> <html> <head> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> </head> <body> <canvas id="myChart"></canvas> <script> const ctx = document.getElementById('myChart'); new Chart(ctx, { type: 'bar', data: { labels: ['Jan', 'Feb', 'Mar'], datasets: [{ label: 'Sales', data: [12, 19, 3], }] } }); </script> </body> </html>
๐ Troubleshooting
Chrome Not Found
Error: Chrome binary not found
Solution:
# Ubuntu/Debian sudo apt install chromium-browser # Set path in config 'chrome_path' => '/usr/bin/chromium-browser',
Timeout Issues
Error: Maximum execution time exceeded
Solution:
PDF::view('large-doc', $data) ->timeout(180) // Increase timeout ->download();
Memory Issues
Solution:
// In php.ini memory_limit = 512M // Or in code ini_set('memory_limit', '512M');
Node.js Not Found
Solution:
# Install Node.js sudo apt install nodejs npm # Or set path 'node_binary' => '/usr/bin/node',
๐ Performance Tips
- Cache views - Use Laravel's view caching
- Queue long PDFs - Use Laravel queues for large documents
- Optimize images - Compress images before including
- Minimize external resources - Inline CSS/JS when possible
- Use local fonts - Avoid loading fonts from CDN
๐งช Testing
# Run tests composer test # Run with coverage composer test-coverage
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
LaraPDFX is open-sourced software licensed under the MIT license.
๐จโ๐ป Author
deecodek
GitHub: @deecodek
โญ Show Your Support
If you find LaraPDFX helpful, please give it a star on GitHub!
๐ Acknowledgments
- Built on top of Spatie Browsershot
- Powered by Chrome/Chromium headless browser
- Inspired by the need for better PDF generation in Laravel
๐ Additional Resources
Made with โค๏ธ for the Laravel community