8

I'm dynamically generating PDFs with an unknown page count. mPDF is working good, but the top margin on the second page is gone. How can I set the margins for all pages with the document?

I've tried the following, but it has no effect:

$mpdf = new mPDF('', '', 0, '', 15, 15, 15, 15, 8, 8);
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74

4 Answers4

23

You can use something like this. it seems to work.

define the margins using @page like:

<?php
    include("mpdf.php");
    $html='<style>@page {
     margin: 0px;
    }</style>


    ';
    $mpdf=new mPDF('','A4');
    $mpdf->WriteHTML($html);
    $mpdf->Output();
    ?>
Srihari Goud
  • 824
  • 10
  • 25
20

I was able to find an answer. Here it is in case anyone needs it:

define the margins using @page like:

@page *{
    margin-top: 2.54cm;
    margin-bottom: 2.54cm;
    margin-left: 3.175cm;
    margin-right: 3.175cm;
}
</style>';

Reference: http://www.mpdf1.com/forum/discussion/80

Klap-in
  • 101
  • 2
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
  • 6
    it also needs to be pointed out that @page selector should be the first selector in the CSS otherwise it can be ignored by mPDF. – Michael Tunnell Jul 25 '13 at 06:15
  • 4
    This did not do anything for me. Removed the asterisk, and it worked. `@page` styles make it impossible to have a header or footer in my experience so far. – Jake Jul 18 '20 at 01:07
  • Hi all, I added more stackoverflow related reference for those who need to do this kind of CSS thing. 1. https://stackoverflow.com/questions/4492432/any-way-to-css-select-all-except-the-first-page 2. https://stackoverflow.com/questions/50833991/css-page-settings-for-second-page I have tested those reference, and it works. – William Luisan Oct 20 '22 at 10:34
4
$mpdf->AddPage('L','','','','',50,50,50,50,10,10);

Reference: Mpdf documentation

0

Faced with the same problem, it helped me to set the margin_footer->0

$mpdf=new \Mpdf\Mpdf(['margin_footer' => 0]);