laravel-dompdf | Display divs as column like flex

laravel-dompdf | Display divs as column like flex

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

Stefan Bogdanescu

Founder & Senior Architect

laravel-dompdf | Display divs as column like flex

When generating PDFs using laravel/dompdf, you quickly discover that the rendering engine operates on a fundamentally different set of rules than a modern browser. The core issue described in many export scenarios—specifically where content shifts down to accommodate a sidebar—is almost always a misunderstanding of how DOMPDF handles CSS layout models.

In a standard web environment, display: flex or display: grid allows containers to align items side-by-side within a single line. However, DOMPDF's CSS parser is not fully compliant with the latest CSS specifications. It supports basic box model properties like float, position, and specific Flexbox utilities, but it often lacks robust support for complex multi-column layouts that rely on advanced flex algorithms or grid areas.

The Root Cause: Table vs. Block Flow

The source material provided shows an attempt to create a layout where the .wrapper acts as a container holding a .sidebar and a main content area. The code sets .wrapper to display: table. While this mimics some table-like behavior, DOMPDF does not interpret nested elements inside a "table" display element exactly how a browser would.

In web browsers, if you have a flex container, children are laid out horizontally by default. In DOMPDF, the rendering engine often defaults to a strict vertical flow for block-level elements unless explicitly told otherwise via floats or tables. When you use display: table on the wrapper in DOMPDF, it attempts to create a table box. However, if the internal structure isn't strictly defined as a <table> element with <tr> and <td> tags, DOMPDF may fail to distribute the width correctly, causing the main content to "float" below the sidebar or push the entire layout down if the engine interprets the sidebar as taking up full vertical space before rendering the next block.

The specific complaint that display: flex is not working is expected. While recent versions of DOMPDF have added experimental support for Flexbox, it is brittle. Properties like flex-direction: row, justify-content, and align-items are recognized, but the engine frequently ignores them when calculating line breaks or when dealing with specific font rendering issues. This leads to unpredictable results where content wraps unexpectedly or aligns vertically rather than horizontally.

The Solution: HTML Table Elements

The most reliable method to achieve a sidebar layout in DOMPDF is to abandon the modern CSS Flexbox model entirely and use native HTML table elements. DOMPDF has been built upon the concept of tables for decades, and its internal rendering engine optimizes for them.

To replicate the desired layout, you should structure your Blade template using <table>, <tr>, and <td> tags instead of .wrapper divs with display: table. Here is how the structure should look:

<table style="width: 100%; border-collapse: collapse;">
    <tr>
        <!-- Sidebar Column -->
        <td class="sidebar" style="width: 5cm; vertical-align: top;">
            <p>{{ $meeting['place'] }}</p>
            <h4><span>Düzenleyen</span></h4>
            <p>{{ $meeting['created_user']['name'] }}</p>
        </td>

        <!-- Main Content Column -->
        <td class="main-content" style="width: 5.7cm; vertical-align: top;">
            <!-- Your main content goes here -->
        </td>
    </tr>
</table>

By using vertical-align: top on the table cells, you ensure that both the sidebar and the main content start at the top of the row. This prevents the "main content shifts down" issue where the engine might try to center the content vertically or push it below the sidebar height if it treats them as separate blocks.

Handling Page Breaks

Another critical aspect mentioned in the source material is the page-break-after: auto class on .wrapper. In DOMPDF, page breaking logic can be finicky. When using tables, you might not need explicit page-break-after classes if your content naturally flows. However, if you must force a break, ensure that the table row itself doesn't span multiple pages unintentionally.

If the sidebar contains long text, the engine might try to push the main content to the next page while keeping the sidebar on the current one, resulting in orphaned headers or cut-off content. To mitigate this, you can apply page-break-inside: avoid to the table cells containing critical headers or images.

.sidebar {
    page-break-inside: avoid;
}
.main-content {
    page-break-inside: avoid;
}

Font and Asset Loading

The source code includes custom font loading via @font-face. DOMPDF requires a specific setup for web fonts to work reliably. Simply linking Google Fonts or loading local .ttf files in the Blade view is often insufficient because DOMPDF runs in its own sandboxed environment (usually using a headless Chrome instance or a dedicated PHP library).

You must ensure that the font paths are accessible from the command line if you are running php artisan pdf:generate. If the fonts are not found, DOMPDF falls back to system defaults, which breaks your typography. It is best practice to embed the CSS directly in the Blade view without relying on external stylesheet links, as DOMPDF sometimes struggles to fetch external resources correctly during generation.

By switching from a fragile Flexbox approach to a robust HTML table structure, you regain precise control over column widths and vertical alignment. This ensures that your meeting details export looks consistent across different operating systems and PDF readers.

Our network