daandekker/laravel-chrome-pdf
Composer 安装命令:
composer require daandekker/laravel-chrome-pdf
包简介
Generate PDFs in Laravel using headless Chrome. Full CSS support, no Node.js required.
README 文档
README
Generate PDFs in Laravel using headless Chrome. Full CSS support including Tailwind CSS, Flexbox, Grid, and modern web features.
Features
- Full CSS support (Flexbox, Grid, Tailwind v4)
- Real browser rendering engine
- System fonts + Google Fonts
- JavaScript support
- Headers & footers with page numbers
- Queued PDF generation
Requirements
- PHP 8.2+
- Laravel 11.0+ or 12.0+
- Chrome/Chromium installed on the server
Installation
composer require daandekker/laravel-chrome-pdf
Optionally publish the configuration:
php artisan vendor:publish --tag=pdf-config
Configuration
Set the Chrome path in your .env:
PDF_CHROME_PATH=/usr/bin/chromium PDF_TIMEOUT=60 PDF_STORAGE_DISK=local PDF_STORAGE_PATH=pdfs
Common Chrome paths:
- Linux:
/usr/bin/chromium,/usr/bin/chromium-browser,/usr/bin/google-chrome - macOS:
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome - Windows:
C:\Program Files\Google\Chrome\Application\chrome.exe - Docker (Alpine):
/usr/bin/chromium-browser
Configuration File
// config/pdf.php return [ 'chrome_path' => env('PDF_CHROME_PATH', '/usr/bin/chromium'), 'timeout' => env('PDF_TIMEOUT', 60), 'storage' => [ 'disk' => env('PDF_STORAGE_DISK', 'local'), 'path' => env('PDF_STORAGE_PATH', 'pdfs'), ], ];
Usage
Basic Usage
use DaanDekker\ChromePdf\Facades\Pdf; // From a Blade view $pdf = Pdf::view('pdf.invoice', ['order' => $order]); // From HTML string $pdf = Pdf::html('<h1>Hello World</h1>');
Output Methods
// Download as attachment return Pdf::view('pdf.invoice', $data)->download('invoice.pdf'); // Display inline in browser return Pdf::view('pdf.invoice', $data)->stream('invoice.pdf'); // Save to file path Pdf::view('pdf.invoice', $data)->save(storage_path('app/invoices/invoice.pdf')); // Save to Laravel storage disk Pdf::view('pdf.invoice', $data)->store('invoices/invoice.pdf', 's3'); // Get raw PDF content $content = Pdf::view('pdf.invoice', $data)->output();
Page Settings
Pdf::view('pdf.invoice', $data) ->format('A4') // A4, A3, Letter, Legal, Tabloid ->orientation('portrait') // portrait, landscape ->landscape() // Shorthand for landscape ->portrait() // Shorthand for portrait ->margins(10, 10, 10, 10) // top, right, bottom, left (mm) ->margin(15) // Uniform margins (mm) ->scale(1.0) // Scale factor (0.1 - 2.0) ->printBackground(true) // Include CSS backgrounds ->download('invoice.pdf');
Headers & Footers
Pdf::view('pdf.invoice', $data) ->header('<div style="font-size: 10px; text-align: center;">Company Name</div>') ->footer('<div style="font-size: 10px; text-align: center;">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>') ->download('invoice.pdf');
Available footer/header variables: pageNumber, totalPages, date, title, url
Wait for JavaScript
// Wait for JS to complete (useful for charts/graphs) Pdf::view('pdf.report', $data) ->waitFor(2000) // milliseconds ->download('report.pdf');
Queued Generation
For large PDFs or background processing:
use DaanDekker\ChromePdf\Jobs\GeneratePdf; GeneratePdf::dispatch( view: 'pdf.invoice', data: ['order' => $order], path: "invoices/order-{$order->id}.pdf", disk: 's3' // optional, defaults to config value );
Styling Tips
Using Tailwind CSS
Include Tailwind via CDN in your Blade template:
<!DOCTYPE html> <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-8"> <h1 class="text-2xl font-bold">Invoice</h1> <!-- Your content --> </body> </html>
Page Breaks
<!-- Force page break before element --> <div style="page-break-before: always;">New Page</div> <!-- Prevent element from breaking across pages --> <div style="page-break-inside: avoid;">Keep together</div>
Print-Specific Styles
@media print { .no-print { display: none; } .print-only { display: block; } }
API Reference
Pdf Facade Methods
| Method | Description |
|---|---|
view(string $view, array $data = []) |
Create PDF from Blade view |
html(string $html) |
Create PDF from HTML string |
PdfBuilder Methods
| Method | Description |
|---|---|
format(string $format) |
Set page format (A4, Letter, etc.) |
orientation(string $orientation) |
Set orientation (portrait/landscape) |
landscape() |
Set landscape orientation |
portrait() |
Set portrait orientation |
margins(int $top, $right, $bottom, $left) |
Set margins in mm |
margin(int $margin) |
Set uniform margins |
scale(float $scale) |
Set scale (0.1 - 2.0) |
printBackground(bool $print = true) |
Enable/disable CSS backgrounds |
header(string $html) |
Set header HTML |
footer(string $html) |
Set footer HTML |
waitFor(int $ms) |
Wait for JS (milliseconds) |
save(string $path) |
Save to file path |
store(string $path, ?string $disk) |
Save to storage disk |
output() |
Get PDF as string |
download(string $filename) |
Return download response |
stream(string $filename) |
Return inline response |
License
MIT License. See LICENSE for details.
daandekker/laravel-chrome-pdf 适用场景与选型建议
daandekker/laravel-chrome-pdf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pdf」 「headless」 「laravel」 「chrome」 「tailwind」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 daandekker/laravel-chrome-pdf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daandekker/laravel-chrome-pdf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 daandekker/laravel-chrome-pdf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This extension provides integration with solr to output content from TYPO3 in JSON format.
Instrument headless chrome/chromium instances from php5.6
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
A new generation of multi-process async event-driven spider engine based on Workerman
Provides TCPDF integration for Symfony
Alfabank REST API integration
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-11