timatanga / content2pdf
timatanga content2Pdf 包。用于从UTF-8编码的内容生成PDF文件
Requires
- php: ^8.0
- mpdf/mpdf: dev-development
Requires (Dev)
- phpunit/phpunit: 10.0.x-dev
- symfony/var-dumper: 6.0.x-dev
This package is auto-updated.
Last update: 2024-09-29 05:50:02 UTC
README
基于不同内容渲染PDF文档是网络应用程序的基本需求。此包是在mPDF PHP库的基础上进行封装,并提供了简化访问方法。
安装
composer require dbisapps/content2pdf
文档
有关PDF渲染选项,请参阅mPDF文档https://mpdf.github.io/。
配置
提供布局模板是支持网络应用程序,使PDF文档具有一致外观和感觉的简单方法。此包支持通过配置文件进行模板配置。请参阅mPDF包以了解支持的选项。
'default' => [
'mode' => 'de_DE',
'format' => 'A4',
'orientation' => 'P',
'margin_left' => 15,
'margin_right' => 15,
'margin_top' => 15,
'margin_bottom' => 15,
'margin_header' => 10,
'margin_footer' => 10,
'default_font_size' => '11',
'default_font' => 'sans-serif',
],
'invoice' => [
],
配置文件名为templates.php,位于packages config目录中。
Laravel配置
在执行Laravel的vendor:publish命令时,配置文件将被复制为template.php到Laravel的config目录。一旦配置已发布,其值可以像其他任何配置文件一样访问。
config('template.default')
将HTML内容渲染为PDF
将HTML内容渲染为PDF非常简单
$pdf = new Html2PDF;
$pdf->include([
'body' =>
'<html><body>
<h1>Test</h1>
<p><img src="///image.jpg"></a></p>
</body></html>'
]);
$output = pdf->stream();
上述命令包括提供的HTML主体,并将PDF作为字符串流。除了模板配置外,还可以作为第二个参数传递给构造函数的自定义选项。请参阅mPdf文档以获取更多信息https://mpdf.github.io/configuration/configuration-v7-x.html
请注意,配置选项将覆盖模板设置。
$pdf = new Html2PDF('default', ['orientation' => 'L']);
除了作为许多页面浮动的给定HTML主体外,提供的页眉和页脚内容将打印在每个页面上。
$pdf = new Html2PDF('default', ['orientation' => 'L']);
$pdf->include([
'header' =>
'<div>This is a header printed on every page</div>',
'body' =>
'<html><body>
<h1>Test</h1>
<p><img src="///image.jpg"></a></p>
</body></html>'
'footer' =>
'<footer>This is a footer printed on every page</footer>',
]);
PDF文档可以通过标题、主题、作者和关键字等元数据来丰富。
$pdf = new Html2PDF('default', ['orientation' => 'L']);
$pdf->include([
'meta' => [
'title' => 'Test Document',
'subject' => 'Just for testing purposes',
'author' => 'John Doe',
'keywords' => ['test', 'html2pdf'],
],
'header' =>
'<div>This is a header printed on every page</div>',
'body' =>
'<html><body>
<h1>Test</h1>
<p><img src="///image.jpg"></a></p>
</body></html>'
'footer' =>
'<footer>This is a footer printed on every page</footer>',
]);
渲染不带样式的HTML页面,听起来很丑陋,但等等,还有一个更选项
$pdf = new Html2PDF('default', ['orientation' => 'L']);
$pdf->include([
'meta' => [
'title' => 'Test Document',
'subject' => 'Just for testing purposes',
'author' => 'John Doe',
'keywords' => ['test', 'html2pdf'],
],
'header' =>
'<div>This is a header printed on every page</div>',
'body' =>
'<html><body>
<h1>Test</h1>
<p><img src="///image.jpg"></a></p>
</body></html>'
'footer' =>
'<footer>This is a footer printed on every page</footer>',
'stylesheet' => '<include the stylesheet here>'
]);
将图像渲染为PDF
当然,图像可以包含在HTML内容中,并按上述方式渲染。当你想到你想以可打印的方式格式化的图像或屏幕截图时,必须有更好的选择。因此,Image2Pdf出现来拯救。
图像大小可以自动约束为当前页面边距,或者在其他情况下被裁剪并在多个页面上分割。虽然图像宽度始终约束在页面边距内,但如果它不适合一个页面,则其高度将被裁剪。
上述所有配置选项都适用于图像内容,除了"body",它只适用于HTML内容。代替"body","image"键开始发挥作用。
$pdf = new Image2Pdf('default',);
$pdf->include([
'header' =>
'<div>This is a header printed on every page</div>',
'image' => 'path/location of image',
'footer' =>
'<footer>This is a footer printed on every page</footer>',
]);
PDF输出
将PDF内容作为字符串流只是几种选项之一。
// finalise the document and return the document as a string
$pdf->stream();
// finalise the document and send the file inline to the browser
$pdf->inline($filename);
// finalise the document and send to the browser and force a file download
$pdf->download($filename);
// finalise the document and save to a local file with the name given by $filename
$pdf->save($filename = 'document.pdf')
访问mPDF功能
可以通过底层的mPdf包提供的任何方法通过魔法PHP方法访问。Html2Pdf和Image2Pdf类都将对未知属性或方法的调用传递给mPdf类。因此,可以将这两个类视为mPDF之上的语法糖。