soheilsam / laravel-pdf
使用此mPDF包装器在Laravel中生成PDF。
Requires (Dev)
- orchestra/testbench: ^3.7
- phpunit/phpunit: ^7.4
README
使用此mPDF包装器,可以直接在Laravel中从HTML生成PDF文档。
安装
在您的composer.json
中要求此包,或者通过运行以下命令安装它
composer require soheilsam/laravel-pdf -w
注意:此包支持Laravel 5.5+的自动发现功能,如果您在低于5.5版本的Laravel上工作,则只需手动添加服务提供者和别名即可
要开始使用Laravel,请将服务提供者和外观添加到您的config/app.php
'providers' => [ // ... niklasravnsborg\LaravelPdf\PdfServiceProvider::class ]
'aliases' => [ // ... 'PDF' => niklasravnsborg\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一起提供的字体(请参阅此处)。
自定义字体
您可以在生成的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
索引。如果您不使用这些索引,您的字符将在生成的PDF中显示为分散和错误。
现在您可以在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许可