padocia / laravel-nova-pdf
从nova资源生成PDF
v1.0.2
2021-07-08 15:27 UTC
Requires
- php: >=7.2.5
- laravel/nova: *
- spatie/browsershot: ^3.41
This package is auto-updated.
Last update: 2024-09-27 02:41:42 UTC
README
✨ 功能
-
导出nova资源到PDF。 使用您设计的blade模板轻松将nova资源导出为PDF文件!可以是发票、报告......天高任鸟飞。
-
PDF文件由blade模板生成。 生成PDF从未如此简单,使用blade编写您的PDF模板并将其导出为PDF。
-
支持Tailwind Css来样式化blade模板。 更喜欢使用Tailwind css框架来样式化您的blade模板?用
.tailwind-container
包裹您的div,即可开始。
要求
此包使用Browsershot,转换在后台由Puppeteer完成,它控制着无头版本的Google Chrome。
安装
您可以通过composer安装此包
composer require padocia/laravel-nova-pdf
如果您在项目中没有puppeteer
npm install puppeteer
您可以发布默认的blade模板
php artisan vendor:publish --tag nova-pdf-template
用法
导出到pdf的nova操作可能由
php artisan nova:ExportToPdfAction InvoiceAction
在app\nova\actions
下创建一个新的nova操作,您可以随意自定义或更改视图
<?php namespace App\Nova\Actions; use Illuminate\Support\Collection; use Illuminate\View\View; use Laravel\Nova\Fields\ActionFields; use Padocia\NovaPdf\Actions\ExportToPdf; class InvoiceAction extends ExportToPdf { /** * @param \Laravel\Nova\Fields\ActionFields $fields * @param \Illuminate\Support\Collection $models * * @return \Illuminate\View\View */ public function preview(ActionFields $fields, Collection $models) : View { $resource = $this->resource; return view('nova-pdf.template', compact('models','resource')); } }
将操作附加到资源
我们将使用User
资源作为示例,将您的操作添加到actions()列表中。
<?php namespace App\Nova; use Illuminate\Http\Request; use App\Nova\Actions\InvoiceAction; class User extends Resource { /** * The model the resource corresponds to. * * @var string */ public static $model = 'App\\User'; // Other default resource methods /** * Get the actions available for the resource. * * @param \Illuminate\Http\Request $request * * @return array */ public function actions(Request $request) { return [ new InvoiceAction, ]; } }
自定义Browsershot
在动作类中重写handleBrowsershotOptions
方法
protected function handleBrowsershotOptions() { $this->browsershot = $this->browsershot->format('A4'); return $this; }
更改文件名
在动作类中重写filename
方法
protected function filename() { return "your_new_filename.pdf"; }
更改磁盘
$this->withDisk('public');
添加新的CSS文件
$this->addStyle('nova_style_name');
移除tailwind css
$this->useTailwind(false);
扩展下载URL过期时间
protected $downloadUrlExpirationTime = 1; // 1 min
更新日志
请参阅更新日志以获取有关最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件zakaria.tayebbey@gmail.com联系。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。