Tcpdf Not Render All CSS Properties

TCPDF not render all CSS properties

In the first place, you should note that PDF and HTML and different formats that hardly have anything in common. If TCPDF allows you to provide input data using HTML and CSS it's because it implements a simple parser for these two languages and tries to figure out how to translate that into PDF. So it's logical that TCPDF only supports a little subset of the HTML and CSS specification and, even in supported stuff, it's probably not as perfect as in first class web browsers.

Said that, the question is: what's supported and what's not? The documentation basically skips the issue and let's you enjoy the trial and error method.

Having a look at the source code, we can see there's a protected method called TCPDF::getHtmlDomArray() that, among other things, parses CSS declarations. I can see stuff like font-family, list-style-type or text-indent but there's no margin or padding as far as I can see and, definitively, there's no float at all.

To sum up: with TCPDF, you can use CSS for some basic formatting. If you need to convert from HTML to PDF, it's the wrong tool. (If that's the case, may I suggest wkhtmltopdf?)

TCPDF - Internal css not working

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

TCPDF does not render external CSS

After researching here and there, I come to know that TCPDF library has few limitations including CSS limitation and does not support many CSS properties. Then, I looked into dompdf library and found it useful. Surprisingly, it solved all my problem with almost similar code and also it is way easy to use than TCPDF. Following is my piece of code i wrote to solve my problem (The code is similar until the $listing variable then i simply change the library code.)

 $listing .= '<style>'.file_get_contents(base_url("styles/style.css")).'</style>';


require_once(APPPATH.'libraries/dompdf/dompdf_config.inc.php');

$dompdf = new DOMPDF();
$dompdf->load_html($listing);
$dompdf->set_paper('a4', 'landscape');
$dompdf->render();

file_put_contents('my_pdf_test.pdf', $dompdf->output());
//$dompdf->stream("dompdf_out.pdf", array("Attachment" => true));
exit(0);

HTML Rendering with TCPDF(PHP)

Are you using tags? tcpdf's HTML engine gives the tag precedence over any CSS, or other size-adjusting tags. If you remove any extraneous tags from the HTML and use straight CSS, things should render as expected. Or, if you aren't using CSS, you should. Just because a browser displays it correctly doesn't mean it will look the same on other formats. The browser has likely performed some magic of its own to fill in the gaps in your CSS specifications.


UPDATE

Here's an example of specifying CSS declarations with your HTML when using tcpdf. Note how all the styling is applied using the CSS declarations inside the <style> tag outside the actualy HTML body.

<?php

$html = <<<EOF
<!-- EXAMPLE OF CSS STYLE -->
<style>
h1 {
color: navy;
font-family: times;
font-size: 24pt;
text-decoration: underline;
}
p {
color: red;
font-family: helvetica;
font-size: 12pt;
}
</style>
<body>
<h1>Example of <i>HTML + CSS</i></h1>
<p>Example of 12pt styled paragraph.</p>
</body>
EOF;

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

?>

TCPDF and issues using added CSS

I've used TCPDF and colspan works perfectly, but i think that (unfortunately) you always have to use inline styles when converting HTML to TCPDF otherwise results might be unpredictable.

I know it's boring and lends to a lot of repetition, but that way colspan and style work perfectly for me.



Related Topics



Leave a reply



Submit