Rectangle 27 4

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.

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, '');

?>

What exactly do you mean? Are you asking me if I define the font size somewhere in the HTML attributes? No. How can I remove all the tags from the HTML?

Some people still erroneously resort to things like <font> tags. What I'm saying is that you should specify all the display logic of your markup using CSS. Make sure you explicitly tell tcpdf what size you want things to display, like body { font-size: large; } or p { font-size: 2em; }

Can I do this with TCPDF parameters somehow? I don't want to mess with the source HTML as this might be adapted to work with 3rd party HTML as well. Can I add some styles seperately, using TCPDF?

Yes, simply declare the style declarations at the beginning of the html -- it's not a best practice to specify HTML inline CSS declarations anyway. I'll update the answer to demonstrate.

I wasn't aware you could specify the CSS on TCPDF's writeHTML(). It isn't even mentioned on the documentation. This answer was of GREAT help, thank you a lot!

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

HTML Rendering with TCPDF(PHP) - Stack Overflow

php html pdf pdf-generation tcpdf
Rectangle 27 4

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.

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, '');

?>

What exactly do you mean? Are you asking me if I define the font size somewhere in the HTML attributes? No. How can I remove all the tags from the HTML?

Some people still erroneously resort to things like <font> tags. What I'm saying is that you should specify all the display logic of your markup using CSS. Make sure you explicitly tell tcpdf what size you want things to display, like body { font-size: large; } or p { font-size: 2em; }

Can I do this with TCPDF parameters somehow? I don't want to mess with the source HTML as this might be adapted to work with 3rd party HTML as well. Can I add some styles seperately, using TCPDF?

Yes, simply declare the style declarations at the beginning of the html -- it's not a best practice to specify HTML inline CSS declarations anyway. I'll update the answer to demonstrate.

I wasn't aware you could specify the CSS on TCPDF's writeHTML(). It isn't even mentioned on the documentation. This answer was of GREAT help, thank you a lot!

HTML Rendering with TCPDF(PHP) - Stack Overflow

php html pdf pdf-generation tcpdf
Rectangle 27 2

If you want to allow only certain tags to be saved in your database you can use strip_tags(). It gives you the possibility to allow certain HTML tags. However, comments and PHP tags will always be stripped.

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');

php - How to sanitize a string without losing HTML and removing JS /SQ...

php sanitization rich-text-editor input-sanitization
Rectangle 27 2

If you want to allow only certain tags to be saved in your database you can use strip_tags(). It gives you the possibility to allow certain HTML tags. However, comments and PHP tags will always be stripped.

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');

php - How to sanitize a string without losing HTML and removing JS /SQ...

php sanitization rich-text-editor input-sanitization