Mohan Kute Mohan Kute - 19 days ago 8
PHP Question

TCPDF - Internal css not working

When I am using following style then everything works properly.

<style>
table{
border: 0.5px solid black;
border-collapse: separate;
border-spacing: 0;
}
</style>


But when Im using class, code is not working.

<style>
.tableWithOuterBorder{
border: 0.5px solid black;
border-collapse: separate;
border-spacing: 0;
}
</style>


Following are my details code.

$htmlData = "<html><head>";
$htmlData .= "<style>
.tableWithOuterBorder{
border: 0.5px solid black;
border-collapse: separate;
border-spacing: 0;
}
</style>";

$htmlData .= "</head><body>";
$htmlData .= "<table class='tableWithOuterBorder'><tr><td>Hello</td><td>Sir</td></tr></table>";
$htmlData .= "</body></html>";

$pdf->writeHTML($htmlData, true, false, false, false, '');


What is wrong in my code?

Answer

Hello Change Below line,

$htmlData   .=  "<table class='tableWithOuterBorder'><tr><td>Hello</td><td>Sir</td></tr></table>";

to

$htmlData   .=  '<table class="tableWithOuterBorder"><tr><td>Hello</td><td>Sir</td></tr></table>';

Just give class name in double quote("), and everything works properly.

My Final code is right now :

$htmlData   =   '<html><head>';
                $htmlData   .=  '<style>
                .tableWithOuterBorder{
                    border: 0.5px solid black;
                    border-collapse: separate;
                    border-spacing: 0;
                }
                </style>';

                $htmlData   .=  '</head><body>';
                $htmlData   .=  '<table class="tableWithOuterBorder"><tr><td>Hello</td><td>Sir</td></tr></table>';
                $htmlData   .=  '</body></html>';
        $pdf->writeHTML($htmlData, true, false, false, false, '');

Thanks