text-align: center on Generate PDF via DomPDF (Laravel) a little bit offset to the right

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Layouts in PDF Generation: Fixing text-align: center Issues with DomPDF

As developers who rely on tools like Laravel to generate dynamic reports and documents, working with PDF generation libraries often introduces subtle rendering quirks. I recently encountered a common frustration when using laravel-dompdf: achieving perfect centering (text-align: center) within complex HTML structures, especially tables, often results in content being slightly offset—a frustrating little misalignment that looks fine on the screen but throws off the final PDF layout.

This post will dive into why this happens and provide a robust, practical solution to ensure your dynamically generated PDFs look perfectly aligned.

The Mystery of the Offset: Why Centering Fails in PDF Generation

The issue you are experiencing—where text-align: center seems correct but the text is slightly offset to the right within the generated PDF—is rarely a bug in the CSS itself; rather, it’s usually an artifact of how the underlying PDF rendering engine (like DomPDF) interprets and translates standard CSS properties into fixed-layout PDF coordinates.

Web browsers are highly forgiving and handle floating elements, margins, and block alignment with complex algorithms. However, PDF generation libraries often rely on a more rigid, coordinate-based system. When you use text-align: center inside table cells (<td>), the centering mechanism might be fighting against the inherent width constraints or padding applied by the table structure itself, leading to that subtle visual shift.

In your specific case, since you are mixing HTML structure with dynamic Blade variables and applying styles directly, the interaction between the CSS rules and the table cell's defined width is causing this minor displacement.

The Solution: Enforcing Centering with Table Structure

The most reliable way to ensure perfect centering within a DomPDF-generated document is to leverage the native capabilities of HTML tables, rather than solely relying on text-align. We need to make the table cell itself behave as a centered container for its content.

Instead of relying only on text-align: center applied directly to the cell, we can refine the styling by ensuring that the content within the cell is handled symmetrically, often by adjusting how the text and image elements are positioned relative to the cell boundaries.

For table cells containing both text and images, applying centering utilities directly to the element—or using a wrapper approach—tends to give DomPDF a clearer instruction on how to manage the space.

Refactoring Your HTML for Perfect Alignment

Looking at your provided view code, specifically where you place the image tags inside <td> elements, we can make the layout more robust. Instead of relying solely on centering text within the cell, let's ensure consistent padding and block behavior.

Here is a conceptual adjustment focusing on your table structure:

<table style="width: 100%;" class="table1">
    <tr>
        <td class="judul" colspan="2">FOTO JAMIN