Mpdf : Set 0 margin for page 1 only

2020-04-17 07:40发布

问题:

$mpdf = new \Mpdf\Mpdf([
        'tempDir' => __DIR__ . '/temp'
    ]);
$mpdf->SetMargins(0, 0, 0); // will set it to 0 for all pages.

Is it possible to have 0 margins for page 1 of a PDF page and default margins for the rest of pages of the document ?

I'm currently testing this with Version 7.0.

回答1:

If you do not need automatic content overflow for page 1 and other pages, you can use AddPageByArray() method:

$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.

In a case of content overflow from the first page, the next, automatically created page will also have zero margins.


Alternatively, you can set zero margins in the constructor and reset margins for following pages using <pagebreak> pseudo-HTML tag:

$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);


标签: php mpdf