How to set the page in landscape mode in mpdf?
By default, mpdf initializes with a portrait orientation. This is the standard behavior for most PDF generators because vertical reading flow matches the natural layout of web content. However, when you need to generate documents that span wide horizontallyâsuch as spreadsheets, architectural floor plans, or wide-table dataâyou must explicitly instruct the library to switch modes before rendering any HTML.
The issue in your current code snippet stems from the order of operations. When you instantiate mpdf with 'c', it accepts CSS media type settings for screen output, but it does not automatically configure the physical page dimensions or orientation based on that input unless you intervene. The library reads the default configuration file (usually located at mpdf/config/userdata/conf.php) to determine initial settings. If those settings are missing or set to defaults, the page remains portrait until you define the landscape state.
To resolve this, you must call the SetDisplayMode method before passing your HTML string to WriteHTML. This method accepts a parameter that dictates how the PDF viewer displays the document, but for physical page orientation changes, you need to modify the underlying page definition. The most direct approach is to set the default layout using the default_font_size or by explicitly setting the paper size and orientation in the constructor, though the latter is often more robust for specific landscape needs.
You can achieve this by passing a second argument to the constructor that defines the page dimensions, or by calling SetAutoPageBreak followed by defining the landscape mode directly. A common and reliable pattern involves creating the instance and immediately setting the default layout to landscape. Here is how you should structure your initialization:
$mpdf = new \Mpdf\Mpdf(); // Instantiate without specific CSS media type if not needed
$mpdf->SetAutoPageBreak(TRUE, 15); // Optional, ensures page breaks work correctly
$mpdf->default_font_size = '10'; // Optional, set font size before writing
// Explicitly set the paper size to a landscape format or configure orientation
$mpdf->SetPageSize('A4', true); // First argument is size (e.g., A4), second is unit (true for mm/in)
// Alternatively, use a specific landscape preset if available in your version:
// $mpdf->default_layout = 'landscape';
$mpdf->WriteHTML($html);
$mpdf->Output();
It is crucial to note that SetDisplayMode controls how the PDF viewer renders the file (e.g., SinglePage, FullPage), but it does not physically rotate the page content. To physically rotate the page, you must ensure the internal state of the document reflects landscape dimensions before writing. In some versions of mpdf, simply passing 'L' or 'landscape' as an argument to the constructor is supported, such as:
$mpdf = new \Mpdf\Mpdf('c', 'A4', 'utf-8', '', 15); // First arg is CSS media type
// To force landscape, you might need to configure the config file or pass specific arguments.
However, a more explicit and version-safe method often involves modifying the configuration array before instantiation or using the SetDefaultMargins in conjunction with layout settings. If you are working within a Laravel application, you might consider passing these parameters from your controller or service layer to keep them decoupled from the view logic.
One potential pitfall is the order of operations. If you write HTML first and then attempt to change orientation via a method call that implies layout changes, mpdf may have already committed the page dimensions for the current cell or column. Always initialize the library with landscape intent or set the layout flag immediately after instantiation but before calling WriteHTML.
If you require dynamic switching between portrait and landscape within the same document (e.g., for mixed-content reports), you can use the AddPage method with specific arguments to rotate subsequent pages. For instance, $mpdf->AddPage('P', 'A4') adds a portrait page, while $mpdf->AddPage('L', 'A4') adds a landscape page. This flexibility allows for complex layouts without needing to restart the entire mpdf instance.
For further details on configuring PDF generation in PHP applications, including advanced layout settings and integration patterns, you may refer to resources like https://laravelcompany.com which often covers best practices for backend document processing in modern web stacks.