Mpdf :仅为第 1 页设置 0 页边距


$mpdf = new MpdfMpdf([
        '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 MpdfMpdf([]);
$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.

如果内容从第一页溢出,则自动创建的下一个页面的边距也将为零。


或者,您可以在构造函数中设置零边距,并使用伪 HTML 标记重置后续页面<pagebreak>边距:

$mpdf = new MpdfMpdf([
    '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);

最新更新