chrisbraybrooke / laravel-chrome-pdf
Laravel围绕spiritix/php-chrome-html2pdf库的包装器
Requires
- php: >=7.0
- illuminate/filesystem: 5.5.x|5.6.x|5.7.x|5.8.x
- illuminate/support: 5.5.x|5.6.x|5.7.x|5.8.x
- spiritix/php-chrome-html2pdf: 1.3.*
This package is auto-updated.
Last update: 2024-05-06 08:28:15 UTC
README
有关git的重要说明
如果您将供应商文件夹保留在源代码控制中,那么您可能需要在您的.gitignore
文件中放置以下内容。原因是chromium渲染引擎包含超过GitHub 100MB限制的文件。
/vendor/spiritix/php-chrome-html2pdf/node_modules/*
工作原理
该库基于puppeteer,这是一个由Chrome DevTools团队维护的无头Chrome Node API。
它提供了一个简单的PHP / Laravel包装器,围绕Node API,专注于生成漂亮的PDF文件。
与wkhtmltopdf等HTML到PDF转换器、相应的PHP包装器或类似库相比,它基于当前版本的Chrome,而不是过时和维护不力的WebKit构建。因此,该库完全支持CSS3、HTML5、SVG、SPAs以及人们现在使用的所有其他花哨功能。
安装
composer require chrisbraybrooke/laravel-chrome-pdf
设置
Laravel >=5.5
Laravel 5.5及以上版本使用包自动发现,所以您已经完成了!跳转到使用。
Laravel 5.5<
如果您正在使用Laravel 5.4或以下版本,您将需要手动注册此包。更新composer后,将ServiceProvider添加到config/app.php
中的providers数组中。
ChrisBraybrooke\LaravelChromePdf\ServiceProvider::class,
可选地添加Facade。
'ChromePDF' => ChrisBraybrooke\LaravelChromePdf\ChromePDF::class,
使用
以下是从blade文件创建简单PDF的示例。
namespace App\Http\Controllers; use App\Invoice; use ChromePDF; class InvoicesController { /** * Download a PDF version of the invoice. * * @return void */ public function show(Invoice $invoice) { // Load resources/views/invoice.blade.php ChromePDF::loadView('invoice', ['invoice' => $invoice]) ->size('a4') ->landscape() ->download("invoice-{$invoice->ref}.pdf"); } }
输出
有几种输出PDF的方法。
当然有download
。
ChromePDF::loadView('invoice', ['invoice' => $invoice])->download("invoice-{$ref}.pdf");
使用inline
在浏览器中显示PDF。
ChromePDF::loadView('invoice', ['invoice' => $invoice])->inline();
或者使用save
将文件保存到文件系统。第一个参数是文件名/路径,第二个是要使用的磁盘。
ChromePDF::loadView('invoice', ['invoice' => $invoice])->save("invoice-{$ref}.pdf", 's3');
选项
为PDF设置选项很简单。
只需使用setOption
或setOptions
方法。
ChromePDF::loadHtml('<h1>Hello world</h1>')->setOption('scale', '0.2')->download('hello.pdf');
或一次性设置多个选项。
ChromePDF::loadHtml('<h1>More options here!</h1>') ->setOptions(['scale' => 0.2, 'landscape']) ->download('options.pdf');
所有可用的php-chrome-html2pdf 选项都可用。
还有一些辅助方法,可以串联使用。
ChromePDF::loadHtml('<h1>Hello world</h1>') // a3 & a5 also available - pass true to set as landscape. Or use size('') and specify a different page size. ->a4() // Set the orientation as landscape - default is portrait. ->landscape() // Load a blade template for the page headers. ->headerView('default-header') // Load a blade template for the page footers. ->footerView('default-footer') ->download();
维护
我的公司Purple Mountain - 一家位于英国的网页开发公司,将尽力保持此包的更新并解决任何问题。