change top margin of second page using mPDF

change top margin of second page using mPDF

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Fixing the Missing Top Margin on Subsequent Pages in mPDF

Generating dynamic PDFs with mPDF often reveals a subtle but frustrating inconsistency: the top margin disappears on every page after the first. This typically happens when content spills over from one page to the next without leaving room for a header or watermark, causing the layout engine to merge the bottom of the previous page directly into the top of the new one. The core issue lies in how mPDF handles page breaks and its default behavior regarding spacing between pages.

The standard constructor signature you might see is $mpdf = new mPDF('', '', 0, '', 15, 15, 15, 15, 8, 8);. In this configuration, the arguments represent margin_left, margin_right, margin_top, and margin_bottom respectively. You might assume that setting these values in the constructor applies uniformly across all pages. While this is true for static content, mPDF's internal logic treats the first page as a special case where it often ignores the top margin if the previous page ended with content touching the bottom edge of its available space.

To enforce a consistent top margin on every page, including the second and subsequent ones, you cannot rely solely on the constructor arguments when dealing with dynamic content that might push content to the absolute bottom of a page. Instead, you must explicitly instruct mPDF to add vertical spacing after each page break. This is achieved using the AddPage method with specific parameters or by manipulating the internal state before rendering new pages.

The most reliable approach within an mPDF script involves setting the margin via the constructor for the base layout but ensuring that the top margin is respected by adding a forced blank space or adjusting the page setup after a break. However, a more direct and robust solution often involves using the SetAutoPageBreak method combined with explicit margin adjustments if the default behavior fails to respect the top margin on page 2+.

Consider this implementation pattern where you define your margins initially but then ensure the document respects them:

$mpdf = new \mpdf\mpdf('', '', 0, '', 15, 15, 15, 15, 8, 8);
$mpdf->SetAutoPageBreak(TRUE, 20); // Adds 20mm space before breaking

// When content forces a new page, the top margin is preserved if auto-break is active.
// If you are manually calling AddPage(), you might need to ensure margins are reset or 
// that the previous page did not end exactly at the bottom limit.

If your dynamic generation logic pushes text all the way to the bottom of the page, mPDF may interpret this as having no space left for a top margin on the next page. To prevent this, you can adjust the margin_bottom slightly or ensure that your content flow does not fill the entire height of the available printable area.

A critical pitfall is assuming that setting margins in the constructor overrides all internal calculations. If you are generating many pages dynamically, mPDF might optimize rendering by skipping the top margin calculation for subsequent pages if it detects no vertical gap was left at the end of the previous page. The solution is to ensure there is always a non-zero margin_bottom or to manually invoke a method that resets the page state.

For projects where consistent branding across all pages is mandatory, such as adding footers or watermarks, failing to address this margin issue can result in overlapping text or missing headers. If you are using mPDF within a Laravel application, ensure that your PDF generation logic explicitly handles page breaks with spacing in mind, rather than relying on implicit defaults that vary based on content density.

When dealing with complex layouts, sometimes the best approach is to slightly increase the margin_bottom in the constructor (e.g., changing the last argument from 8 to 10) to create a buffer. This forces mPDF to recognize that the previous page did not consume the entire vertical space, thereby reserving room for the top margin on the following page.

Ultimately, understanding mPDF's rendering model is key: it respects margins only when there is defined space between pages. By ensuring your content does not fill the page to the absolute limit or by explicitly configuring auto-page-break spacing, you can maintain consistent top margins throughout the entire document.

Our network