famelo / pdf
通过 MPDF 库提供了一种快速简单的方式,从 Fluid 模板生成 PDF
4.2.0
2021-08-25 14:06 UTC
Suggests
- itbz/fpdi: fpdi library to use the FpdiGenerator
- knplabs/knp-snappy: wkhtmltopdf wrapper to use the WebkitGenerator
README
此软件包通过 MPDF 库提供了一种快速简单的方式,从 Fluid 模板生成 PDF
示例
$document = new \Famelo\PDF\Document('My.Package:SomeDocument'); $document->assign('someVariable', 'foobar'); // Trigger a Download and exit $document->download('SomeDocument ' . date('d.m.Y') . '.pdf'); // Show the document inline and exit $document->send(); // Save the document to a local file $document->save('/Some/Path/SomeDocument ' . date('d.m.Y') . '.pdf');
此示例将渲染位于 'resource://My.Package/Private/Documents/SomeDocument.html' 的模板并将其转换为 PDF。
页面格式和方向
默认情况下,页面将以 A4 竖向格式渲染。您可以选择其他格式/方向,如下所示
// set format to A5 Portrait $document = new \Famelo\PDF\Document('My.Package:SomeDocument', 'A5'); // set format to an A4 Landscape $document->setFormat('A4-L'); // set format to 100mm x 200mm $document->setFormat(array(100, 200));
MPDF 库支持以下关键字或包含宽度和高度值的数组来支持不同的页面大小
- A0 - A10
- B0 - B10
- C0 - C10
- 4A0
- 2A0
- RA0 - RA4
- SRA0 - SRA4
- Letter
- Legal
- Executive
- Folio
- Demy
- Royal
- Ledger
- Tabloid*
所有上述值都可以后缀为"-L"来强制页面方向为横向文档,例如 "A4-L"。如果格式定义为字符串,则最终方向参数将被忽略。
*Ledger 和 Tabloid 是具有相同页面大小但方向不同的标准格式(Ledger 为横向,Tabloid 为纵向)。mPDF 将它们视为相同;如果您想使用 Ledger,应该指定 "Ledger-L" 以进行横向。
Fluid 中的页面编号
如果您想使用 mPDF 的页面编号变量,如 {PAGENO} 或 {nbpg},则必须将它们包裹在 CDATA 部分,以防止 Fluid 将其解释为变量。
PDF 生成器实现
默认情况下,此库使用 MPDF 库生成 PDF。但您可以通过 Settings.yaml 中的 defaultGenerator 来更改默认生成器
Famelo: PDF: # Generator using wkhtmltopdf through knplabs/knp-snappy DefaultGenerator: '\Famelo\PDF\Generator\WebkitGenerator' DefaultGeneratorOptions: Binary: '/usr/local/bin/wkhtmltopdf'
请随意创建并使用您喜欢的 PDF 库的生成器。如果您认为其他人可能会喜欢使用它,请向我发送 Pull-Request :)