【发布时间】:2018-04-19 11:49:17
【问题描述】:
$mpdf = new \Mpdf\Mpdf([
'tempDir' => __DIR__ . '/temp'
]);
$mpdf->SetMargins(0, 0, 0); // will set it to 0 for all pages.
是否可以为 PDF 页面的第 1 页设置 0 页边距,为文档的其余页面设置默认页边距?
我目前正在使用 7.0 版进行测试。
【问题讨论】:
$mpdf = new \Mpdf\Mpdf([
'tempDir' => __DIR__ . '/temp'
]);
$mpdf->SetMargins(0, 0, 0); // will set it to 0 for all pages.
是否可以为 PDF 页面的第 1 页设置 0 页边距,为文档的其余页面设置默认页边距?
我目前正在使用 7.0 版进行测试。
【问题讨论】:
如果第1页和其他页面不需要自动内容溢出,可以使用AddPageByArray()方法:
$mpdf = new \Mpdf\Mpdf([]);
$mpdf->AddPageByArray([
'margin-left' => 0,
'margin-right' => 0,
'margin-top' => 0,
'margin-bottom' => 0,
]);
$mpdf->WriteHTML($html1); // first page
$mpdf->AddPageByArray([
'margin-left' => '15mm',
'margin-right' => '20mm',
'margin-top' => '15mm',
'margin-bottom' => '15mm',
]);
$mpdf->WriteHTML($html2); // other pages
// All other pages will then have margins of the second `AddPageByArray()` call.
如果第一页内容溢出,下一个自动创建的页面也将具有零边距。
或者,您可以在构造函数中设置零边距,并使用 <pagebreak> 伪 HTML 标记重置后续页面的边距:
$mpdf = new \Mpdf\Mpdf([
'margin_left' => 0,
'margin_right' => 0,
'margin_top' => 0,
'margin_bottom' => 0,
]);
$html = 'Content of the first page
<pagebreak margin-left="15mm" margin-right="15mm" margin-top="15mm" margin-bottom="20mm">
Other content';
$mpdf->WriteHTML($html1);
【讨论】: