How to setting landscape in laravel-dompdf?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# How to Setting Landscape in Laravel-Dompdf: A Developer's Guide Dealing with document generation, especially when dealing with specific page orientations like landscape, can often prove frustrating. Many developers encounter this exact issue when trying to integrate PDF libraries into their Laravel applications, such as using `laravel-dompdf`. You are seeing that setting the configuration in `config/dompdf` is not yielding the desired result, leaving you wondering where the actual control lies. As a senior developer, I can tell you that often, the solution isn't purely about tweaking a configuration file; it involves understanding how the underlying PDF rendering engine (Dompdf) interprets layout instructions and applying those instructions through CSS. This post will walk you through why your attempt might be failing and provide the robust, proven method for forcing landscape orientation in your generated PDFs using `laravel-dompdf`. ## The Pitfall of Configuration Settings You correctly identified that you tried setting the orientation in your configuration file: ```php // config/dompdf.php (Attempted Setting) 'orientation' => 'landscape', ``` While this approach seems logical, in many wrapper packages like `laravel-dompdf`, the configuration array might control general settings rather than forcing a specific page geometry directly, especially when dealing with complex HTML layouts. If the configuration setting is ignored or overridden by default PDF rendering behaviors, you end up back where you started—a portrait document. The key takeaway here is that CSS, not just configuration flags, dictates the physical layout of the printed page. To truly control the size and orientation of the output in Dompdf, we need to instruct the HTML/CSS framework on how the physical page should be structured. ## The Correct Approach: Mastering CSS for Page Orientation The most reliable way to force a landscape orientation is by using CSS media queries or, more effectively for fixed document dimensions, the `@page` rule within your generated HTML content. This tells the PDF renderer exactly how the paper boundaries should be defined before rendering the content. Here is the step-by-step process: ### Step 1: Adjusting the View Content (HTML/CSS) Instead of relying solely on a global configuration setting, you must embed the specific page size requirement directly into the HTML view that `laravel-dompdf` will convert. For landscape mode, we typically define the width and height based on standard paper sizes (e.g., A4 or Letter). For landscape A4, the dimensions change from Portrait (210mm x 297mm) to Landscape (297mm x 210mm). You need to adjust your container size accordingly. **Example HTML Structure:** ```html Landscape PDF

Landscape Report

This content is now rendered in landscape orientation.

``` ### Step 2: Controller Implementation Check Your controller logic remains sound, as it correctly calls the PDF loading method provided by the package. The focus shifts entirely to ensuring the view being loaded contains the necessary CSS instructions for Dompdf to follow. ```php // Example Controller Code (Remains correct) use Barryvdh\DomPDF\Facade\Pdf; public function listdata() { $pdf = PDF::loadView('pdf.landscape_report'); // Ensure this view uses the landscape CSS return $pdf->stream('test_pdf.pdf'); } ``` ## Best Practices and Laravel Context When working with document generation in a Laravel environment, remember that you are leveraging PHP to orchestrate an external rendering engine. This mirrors the principles of application architecture we discuss at `laravelcompany.com`. The package acts as an abstraction layer, but the final output quality is determined by the data (HTML/CSS) you feed it. **Key Takeaways for Developers:** 1. **Prioritize CSS:** Always test layout issues by manipulating the embedded CSS (`@page` rules). This provides the most direct control over the PDF dimensions. 2. **Test Paper Sizes:** If you are working with custom or non-standard paper sizes, ensure your CSS values align with those specifications. 3. **Debugging Strategy:** If