TCPDF not render all CSS properties

后端 未结 10 1819
不思量自难忘°
不思量自难忘° 2020-12-05 09:15

I try to make a PDF writing CSS and HTML but my CSS doesn\'t appear in my PDF. The only thing considered is the font-size and font-color.

I give you the code (sorry,

相关标签:
10条回答
  • 2020-12-05 09:55

    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?)

    0 讨论(0)
  • 2020-12-05 09:55

    Supported tags are: a, b, blockquote, br, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, img, li, ol, p, pre, small, span, strong, sub, sup, table, tcpdf, td, th, thead, tr, tt, u, ul

    All the HTML attributes must be enclosed in double-quotes

    0 讨论(0)
  • 2020-12-05 09:55

    TCPDF 6.2.11 (2015-08-02)

    Some things won't work when included within <style> tags, however they will if added in a style="" attribute in the HTML tag. E.g. table padding – this doesn't work:

    table {
        padding: 5px;
    }
    

    This does:

    <table style="padding: 5px;">
    
    0 讨论(0)
  • 2020-12-05 10:00

    Just a small tip for setting custom padding without extra table elements. Just use this way, it works (TCPDF 6.2.11)

    <table border="0" style="padding-left: 10px; padding-bottom: 15px;">
    <tr>
    <td style="border: 1px solid grey;"> One two three </td>
    <td style="border: 1px solid grey;"> Four five six </td>
    </tr>
    </table>
    
    0 讨论(0)
  • 2020-12-05 10:06

    Since version 5.7 TCPDF includes support for CSS borders. Margins, padding and float are not yet supported. Check the TCPDF website at http://www.tcpdf.org and consult the official forum for further information.

    0 讨论(0)
  • 2020-12-05 10:06

    I found this:

    // Remove tag bottom and top margins
    
    $tagvs = array( 'p' => array( 
                                  0 => array('h' => 0, 'n' => 0), 
                                  1 => array('h' => 0, 'n' => 0)
                                 ) 
                  );
    $pdf->setHtmlVSpace($tagvs);
    

    in here: https://tcpdf.org/examples/example_061/

    Use it to remove 'p' tags properties (bottom and top), then position the 'p' text inside a cell.

    0 讨论(0)
提交回复
热议问题