Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mpdf : Set 0 margin for page 1 only

Tags:

php

mpdf

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

like image 656
anjanesh Avatar asked Oct 27 '25 18:10

anjanesh


1 Answers

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);
like image 87
Finwe Avatar answered Oct 29 '25 09:10

Finwe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!