zanysoft / laravel-pdf
Laravel PDF - 使用 mPDF 包装器生成 PDF 发票或文档。
Requires
- php: ^8.0
- mpdf/mpdf: ^8.0.10
This package is auto-updated.
Last update: 2024-09-21 09:53:10 UTC
README
使用此 PDF 包装器,可以直接在 Laravel 中从 HTML 生成 PDF 文档。
内容
安装指南
在您的 composer.json
中要求此包或运行以下命令进行安装:
composer require zanysoft/laravel-pdf
要开始使用 Laravel,将服务提供者和外观添加到您的 config/app.php
'providers' => [ // ... ZanySoft\LaravelPDF\PdfServiceProvider::class ]
'aliases' => [ // ... 'PDF' => ZanySoft\LaravelPDF\Facades\PDF::class ]
配置
默认配置设置在 config/pdf.php
中。将此文件复制到您的配置目录以修改值。您可以使用以下命令发布配置:
php artisan vendor:publish --provider="ZanySoft\LaravelPDF\PdfServiceProvider"
基本用法
要使用 Laravel PDF,请将以下内容添加到您的控制器中之一。您可以将数据传递给 /resources/views
中的视图。
use PDF; function generate_pdf() { $data = [ 'foo' => 'bar' ]; $pdf = PDF::Make('document.pdf'); $pdf->loadView('pdf.document', $data); return $pdf->stream(); }
或
use ZanySoft\LaravelPDF\PDF; function generate_pdf() { $data = [ 'foo' => 'bar' ]; $pdf = new PDF(); $pdf->loadView('pdf.document', $data); return $pdf->stream('document.pdf'); }
如果您想从 HTML 内容生成
$content = "Hello this is first pdf file." $pdf->loadHTML($content); return $pdf->stream('document.pdf');
如果您想从文件生成
$file = "file.txt" $pdf->loadFile($file); return $pdf->stream('document.pdf');
如果您想下载 PDF 文件
return $pdf->embed('document.pdf');
如果您想将 PDF 保存到服务器
return $pdf->save('with-complete-path/document.pdf');
如果您想将 PDF 文件作为附件添加到电子邮件
return $pdf->embed('document.pdf');
页眉和页脚
如果您想在每一页上显示页眉和页脚,请将其添加到您的 <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 [ 'custom_font_path' => base_path('/resources/fonts/'), // don't forget the trailing slash! ];
然后
$fontdata = array( '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 ] // ...add as many as you want. ); $pdf->addCustomFont($fontdata, true); // If your font file is unicode and "OpenType Layout" then set true. Default value is false.
现在您可以在 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::Make(); $pdf->SetProtection(['copy', 'print'], 'user_pass', 'owner_pass') $pdf->loadView('pdf.document', $data); return $pdf->stream('document.pdf'); }
有关 SetProtection()
的更多信息,请参阅此处:https://mpdf.github.io/reference/mpdf-functions/setprotection.html
文档
有关更多选项和设置,请参阅此链接:https://mpdf.github.io/