DomPDF table fixed column width and break long text

DomPDF table fixed column width and break long text

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

Stefan Bogdanescu

Founder & Senior Architect

DomPDF table fixed column width and break long text

Handling tables in PDF generation is notoriously fragile, particularly when dealing with long strings of text that lack natural whitespace, such as full URLs or parameter-heavy data. When using DomPDF within a Laravel application via barryvdh/laravel-dompdf, developers often encounter a specific conflict: the CSS properties word-break and word-wrap function correctly to split long text, but the column width constraints defined in the HTML table structure frequently collapse or expand to fit that content.

The core issue stems from how CSS defines table layout. Without explicit intervention, browsers (and rendering engines like PhantomJS used by DomPDF) default to an auto-layout algorithm. In this mode, columns are sized based on their content. If a cell contains a 500-character string without spaces, the column expands indefinitely, rendering fixed width styles ineffective.

To force specific column widths, you must apply table-layout: fixed to the table element itself. This tells the layout engine to ignore content length when calculating column dimensions and strictly adhere to the width attribute or style set on the <th> and <td> elements. Once this flag is active, the HTML structure respects your 300px or 25px constraints regardless of what text sits inside.

However, applying table-layout: fixed introduces a secondary problem: the rendering engine will not automatically shrink the cell to fit the content if it exceeds the width. Instead, the text spills over into subsequent columns, creating a horizontal overflow that breaks the document layout. This is why simply adding word-break: break-all and word-wrap: break-word to your data cells often feels insufficient; the visual result still appears broken because the column itself has no defined boundary to stop at.

The standard solution involves combining fixed table layout with CSS overflow handling. To prevent text from bleeding into adjacent columns when it exceeds the width, you need to apply overflow: hidden to the specific <td> elements containing long text. This clips the content strictly within the column's defined bounds. If you require the text to be fully visible and prefer it to appear on a new line rather than being clipped, you can instead allow the cell to grow vertically by removing horizontal overflow constraints, though this may increase page height significantly.

A more aggressive approach involves using white-space: pre-wrap alongside overflow-x: hidden. This combination preserves line breaks (useful when processing data with nl2br) while ensuring that any text exceeding the column width is cut off cleanly without affecting the table's structural integrity.

Here is how you should structure your Blade template to achieve a stable result:

<table class="bordered" style="table-layout: fixed; border-collapse: collapse;">
    <thead>
        <tr>
            <th style="width: 25px; border: 1px solid #ccc;">No</th>
            <th style="width: 100px; border: 1px solid #ccc;">Nama Alat</th>
            <th style="width: 25px; border: 1px solid #ccc;">Jml</th>
            <!-- Apply width and overflow directly to the th/td -->
            <th style="width: 300px; border: 1px solid #ccc;">Spesifikasi</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($items as $index => $item)
        <tr>
            <td style="width: 25px; border: 1px solid #ccc;">{{ ($index+1) }}</td>
            <td style="width: 100px; border: 1px solid #ccc;">{{ $item['name'] }}</td>
            <td style="width: 25px; border: 1px solid #ccc;">{{ $item['jumlah_disetujui'] }}</td>
            
            <!-- Critical fix: overflow hidden clips text to column width -->
            <td style="width: 300px; border: 1px solid #ccc; word-break: break-all; word-wrap: break-word; overflow: hidden;">
                {!! nl2br( $item['spesifikasi'] ) !!}
            </td>
            
            <!-- Critical fix: overflow hidden clips text to column width -->
            <td style="width: 300px; border: 1px solid #ccc; word-break: break-all; word-wrap: break-word; overflow: hidden;">
                {!! nl2br( $item['supplier'] ) !!}
            </td>

            <td style="width: 150px; border: 1px solid #ccc;">
                @if ($item['image'])
                    <img style="max-height: 125px;" src="{{ $item['image'] }}" />
                @else
                    -
                @endif
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

The overflow: hidden property is the missing piece that reconciles the fixed column width with the need to break long text. Without it, DomPDF (like standard browsers) will honor the content's natural width over your CSS definition. With it, the column remains exactly 300px wide, and the text breaks or clips accordingly, preserving the grid structure.

If this approach still fails for specific complex layouts, it is worth noting that DomPDF has historically struggled with advanced table layout algorithms. Issues regarding cell width calculation (#1017) and fixed layout ignored column widths (#1432) are documented in the project's repository. In such scenarios, switching to a library like TCPDF or mPDF might be necessary, though they come with their own learning curves and API differences. For most standard reporting needs within Laravel, however, combining table-layout: fixed with overflow: hidden provides a reliable workaround.

Our network