meeeet-dev / laravel-pdf
使用这个mPDF包装器在Laravel中生成PDF。
Requires
- php: ^7.0|^8.0
- mpdf/mpdf: ^8.0
Requires (Dev)
- orchestra/testbench: ^3.7
- phpunit/phpunit: ^7.4
README
使用此mPDF包装器,您可以在Laravel中轻松地从HTML生成PDF文档。
安装
在您的 composer.json
中要求此包,或者通过运行来安装
composer require meeeet-dev/laravel-pdf
注意:此包支持Laravel 5.5+的自动发现功能,如果您使用的是低于5.5版本的Laravel,则需要手动添加服务提供者和别名
要开始使用Laravel,请将服务提供者和外观添加到您的 config/app.php
'providers' => [ // ... MeeeetDev\LaravelPdf\PdfServiceProvider::class ]
'aliases' => [ // ... 'PDF' => MeeeetDev\LaravelPdf\Facades\Pdf::class ]
现在,您应该使用以下命令将包的配置文件发布到您的配置目录
php artisan vendor:publish
基本用法
要使用Laravel PDF,请将类似以下内容添加到您的控制器中。您可以将数据传递到 /resources/views
中的视图。
use PDF; function generate_pdf() { $data = [ 'foo' => 'bar' ]; $pdf = PDF::loadView('pdf.document', $data); return $pdf->stream('document.pdf'); }
其他方法
您还可以在 pdf
对象上使用以下方法
output()
:以字符串形式输出PDF。
save($filename)
:将PDF保存到文件。
download($filename)
:使PDF可供用户下载。
stream($filename)
:返回一个响应,其中包含要在浏览器中显示的PDF。
配置
如果您已发布配置文件,您可以在 config/pdf.php
文件中更改默认设置
return [ 'format' => 'A4', // See https://mpdf.github.io/paging/page-size-orientation.html 'author' => 'John Doe', 'subject' => 'This Document will explain the whole universe.', 'keywords' => 'PDF, Laravel, Package, Peace', // Separate values with comma 'creator' => 'Laravel Pdf', 'display_mode' => 'fullpage' ];
要基于文件覆盖此配置,请使用初始化调用的第四个参数,如下所示
PDF::loadView('pdf', $data, [], [ 'format' => 'A5-L' ])->save($pdfFilePath);
您可以使用带有键 'instanceConfigurator' 的回调来访问mpdf函数
$config = ['instanceConfigurator' => function($mpdf) { $mpdf->SetImportUse(); $mpdf->SetDocTemplate(/path/example.pdf, true); }] PDF::loadView('pdf', $data, [], $config)->save($pdfFilePath);
页眉和页脚
如果您想要在每一页上显示页眉和页脚,请将其添加到您的 <body>
标签中,如下所示
<htmlpageheader name="page-header"> Your Header Content </htmlpageheader> <htmlpagefooter name="page-footer"> Your Footer Content </htmlpagefooter>
现在,您只需要使用CSS中的名称属性来定义它们
@page { header: page-header; footer: page-footer; }
在页眉和页脚中,可以使用 {PAGENO}
来显示页码。
包含的字体
默认情况下,您可以使用mPDF附带的所有字体 shipped with mPDF。
自定义字体
您可以在生成的PDF中使用自己的字体。TTF文件必须位于一个文件夹中,例如 /resources/fonts/
。将其添加到您的配置文件中(/config/pdf.php
)
return [ // ... 'font_path' => base_path('resources/fonts/'), 'font_data' => [ 'examplefont' => [ 'R' => 'ExampleFont-Regular.ttf', // regular font 'B' => 'ExampleFont-Bold.ttf', // optional: bold font 'I' => 'ExampleFont-Italic.ttf', // optional: italic font 'BI' => 'ExampleFont-Bold-Italic.ttf' // optional: bold-italic font //'useOTL' => 0xFF, // required for complicated langs like Persian, Arabic and Chinese //'useKashida' => 75, // required for complicated langs like Persian, Arabic and Chinese ] // ...add as many as you want. ] // ... ];
注意:如果您使用 laravel-pdf
生成复杂语言(如波斯语、阿拉伯语或中文)的PDF文档,您应该在自定义字体定义数组中具有 useOTL
和 useKashida
索引。如果您不使用这些索引,您的字符将错误地显示为分散的。
现在您可以在CSS中使用该字体
body { font-family: 'examplefont', sans-serif; }
设置保护
要设置保护,只需调用 SetProtection()
方法并传递一个包含权限、用户密码和所有者密码的数组。
密码是可选的。
有一些权限: 'copy'
、'print'
、'modify'
、'annot-forms'
、'fill-forms'
、'extract'
、'assemble'
、'print-highres'
。
use PDF; function generate_pdf() { $data = [ 'foo' => 'bar' ]; $pdf = PDF::loadView('pdf.document', $data); $pdf->SetProtection(['copy', 'print'], '', 'pass'); return $pdf->stream('document.pdf'); }
有关 SetProtection()
的更多信息,请参阅:https://mpdf.github.io/reference/mpdf-functions/setprotection.html
测试
要使用测试套件,您需要在本地PHP上安装一些扩展和二进制文件。在macOS上,您可以像这样安装它们
brew install imagemagick ghostscript
pecl install imagick
许可证
Laravel PDF是开源软件,许可协议为MIT许可证