Search code examples
phpdompdf

Simple tables not rendering correctly in PDF with DOMPDF converter


I am using DOMPDF to convert HTML to PDF. Two simple tables that are next to one another on the browser don't render on PDF the same, i.e. the second table appears in a new line. Here is the HTML:

<html>
<head>
    <style>
        .simple_table { 
            width: 200px;
            float: left; 
        }
    </style>
</head>
<body>
        <div class="simple_table">
             <table >
                <tr><td>table1 </td></tr>
                <tr><td>table1 </td></tr>
            </table>
        </div>
        <div class="simple_table">
            <table >
                <tr><td>table2</td></tr>
                <tr><td>table2</td></tr>
            </table>
        </div>
</body>
</html> 

And the PHP to invoke DOMPDF converter:

$dompdf = new DOMPDF();
$html = file_get_contents("http://linkToAboveHTML");
$dompdf->load_html($html);
$dompdf->set_paper('A4','potrait');
$dompdf->render();
$dompdf->stream("dompdf.pdf");

Solution

  • Float support is still an "experimental" feature of dompdf as of v0.6.1, and it's disabled by default. You can enable float support by setting the DOMPDF_ENABLE_CSS_FLOAT configuration constant to true.

    Starting in 0.6.1 you can also change settings in the instantiated object by calling the set_option method, e.g. $dompdf->set_option('enable_css_float',true).