pxlrbt / laravel-pdfable
这是我的包laravel-pdfable
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
Suggests
- spatie/browsershot: ^3.57.5
This package is auto-updated.
Last update: 2024-09-08 22:37:01 UTC
README
像Laravel的Mailables一样,将您的PDF逻辑放在一个地方。
安装
您可以通过composer安装此包
composer require pxlrbt/laravel-pdfable
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="pdfable-config"
可选地,您可以使用以下命令发布视图
php artisan vendor:publish --tag="pdfable-views"
配置
目前支持两种驱动程序
- 浏览器截图(默认)
- Wkhtmltopdf(旧版,wkhtmltopdf已弃用)
浏览器截图驱动程序
这是默认驱动程序,需要spatie/browsershot。请按照该包的安装说明进行操作。
您可以通过在您的 AppServiceProvider
中调用 BrowsershotDriver::configureUsing()
来配置浏览器截图驱动程序
BrowsershotDriver::configureUsing( fn (Browsershot $browser) => $browser->setCustomTempPath(storage_path('tmp')) );
Wkhtmltopdf 驱动程序
要使用wkhtmlpdf驱动程序,请确保您的系统已安装wkhtmltopdf并全局可用。
然后,将 PDFABLE_DRIVER
选项在您的 .env
文件中设置为 wkhtmltopdf
。
生成Pdfables
您可以使用make命令生成Pdfable类和视图。
php artisan make:pdf Invoice
用法
您可以在您的应用程序的许多地方直接使用、传递或返回Pdfables。
作为文件
您可以通过 ->store()
方法存储Pdfables。这将使用类上的 outputFile()
方法来确定类名。可选地,您也可以传递一个自定义的文件名。
(new Invoice($order)->store()));
作为响应
您可以流式传输、下载或返回您的Pdfables HTML进行调试。
HTML
要返回调试视图中的HTML,只需返回Pdfable即可。
Route::get('/invoice/{order}', fn (Order $order) => new Invoice($order));
流
要流式传输您的Pdfable,请添加 ->stream()
方法。
Route::get('/invoice/{order}', fn (Order $order) => (new Invoice($order)->stream()));
下载
要下载您的Pdfable,请添加 ->download()
方法。可选地,您也可以从这里覆盖文件名。
Route::get('/invoice/{order}', fn (Order $order) => (new Invoice($order)->download('custom-filename.pdf')));
作为邮件附件
要将Pdfable用作邮件附件,只需通过 ->attach()
传递即可。请确保您的mailables/notifications已排队以加快处理速度。
return (new MailMessage) ->subject("Your Invoice") ->attach(new Invoice($order));
作为作业
PDF的生成可能需要一些时间,因此您可以将Pdfables排队,并在后台使用已知的Laravel方法创建它们。
dispatch(new Invoice($order)); // or Invoice::dispatch($order); // ...
编写Pdfables
一旦生成了pdfable类,打开它,我们可以探索其内容。Pdfable类配置是通过几个方法完成的。
配置视图
视图是通过静态 $view
属性配置的。
class Invoice extends Pdfable { public string $view = 'pdf.task'; }
配置页面/布局
您可以通过返回一个 Page
对象来配置PDF页面大小、方向和边距。
public function page(): Page { return Page::make()->size(PageSize::A4)->margins('narrow'); }
传递额外数据
通过您的Pdfable构造函数传递额外数据以供以后使用。
public function __construct( public Order $order, public ?Customer $customer = null, ) {}
从视图中访问数据
类似于Laravel的Blade组件,您可以直接从您的视图文件访问属性和公共方法。
<h1>Invoice for Order {{ $order->id }}</h1> <div>Total: {{ $getTotal() }}</div>
配置输出文件
当将Pdfable保存到磁盘时,您可以通过 filename()
提供默认路径,并通过 $disk
属性覆盖默认磁盘。
public function filename(): string { return "customers/{$this->customer->id}/{$this->order->id}.pdf"; }
排队一个Pdfable
Pdfables实现了 ShouldQueue
,因此可以通过 Invoice::dispatch()
推送到队列。您还可以直接在您的Pdfable上使用其他队列配置方法,如 backoff()
、retryUntil()
、uniqueId()
等。
鸣谢
许可证
麻省理工学院许可证(MIT)。有关更多信息,请参阅许可文件。