TCPDF不渲染所有CSS属性 [英] TCPDF not render all CSS properties

查看:408
本文介绍了TCPDF不渲染所有CSS属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PDF编写CSS和HTML,但我的CSS不会出现在我的PDF中。唯一要考虑的是字体大小和字体颜色。

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, it's a little long...)

$config = sfTCPDFPluginConfigHandler::loadConfig('my_config');

    $doc_title    = "Fiche Logement";

    $html = <<<EOF
<style>
        .informations {
            padding: 10px;
            margin: 10px;
            border: 1px dotted black;}
        .informations table {
            margin-top: 10px;}
        #modif {
            margin: 20px;
            text-align: left;
            float: right;}
        #modif th {
            padding-left: 10px;}
        #modif td {
            padding-left: 10px;}
        #adresse {
            width: 307px;
            float: left;}
        #reservataire {
            width: 307px;
            float: right;}
        #intergen {
            width: 307px;
            float: right;}
        #infos {
            width: 290px;
            float: left;}
        #handicap {
            padding-bottom: 12px;
            width: 324px;
            float: right;}
        #charges {
            margin-bottom: 20px;
            width: 307px;
            float: left;}
        #loyer {
            width: 307px;
            float: right;}
        #commentaires {
            clear: both;}
        h2 {
            font-variant: small-caps;
            text-align: center;
            font-size: 19px;
            font-weight: bold;
            padding: 0px 0px 2px 5px;
            margin: 15px 0px 20px 0px;
            color: #000000;
            border-top: 1px dotted black;
            border-bottom: 1px dotted black;}

        h3 {
            width: 250px;
            font-variant: small-caps;
            font-size: 15px;
            font-weight: bold;
            padding: 0px 0px 0px 5px;
            margin: 0px;
            color: #225D6D;
            border-top: 1px dotted black;
            border-bottom: 1px dotted black;}
</style>


            <div id='intergen' class='informations'>
                <h3>Intergénérationnel</h3>
                <table>
                    <tr>
                        <th>Intergénérationnel :</th> <td><?php echo \$logement->getIntergen() ?></td>
                    </tr>
                </table>
            </div>

            <div id='infos' class='informations'>
                <h3>Informations logement</h3>
                <table>
                    <tr>
                        <th>Bâtiment :</th> <td><?php echo \$logement->Parclogement->getBatiment() ?></td>
                    </tr>
                    <tr>
                        <th>Taille :</th> <td><?php echo \$logement->getTaille() ?></td>
                    </tr>
                    <tr>
                        <th>Type :</th> <td><?php echo \$logement->getTypelog() ?></td>
                    </tr>
                    <tr>
                        <th>Surface habitable :</th> <td><?php if(\$logement->getSurfacehab() == 0){ echo 'Non Spécifié';} else {echo \$logement->getSurfacehab(). " m²";} ?></td>
                    </tr>
                    <tr>
                        <th>Chauffage :</th> <td><?php echo \$logement->getChauffage() ?></td>
                    </tr>
                    <tr>
                        <th>Ascenseur :</th> <td><?php echo \$logement->getAscenseur() ?></td>
                    </tr>
                </table>
            </div>

            <div id='handicap' class='informations'>
                <h3>Infrastructures handicapés</h3>
                <table>
                    <tr>
                        <th>Immeuble accessible :</th> <td><?php echo \$logement->getAccessibl() ?></td>
                    </tr>
                    <tr>
                        <th>Ascenceur accessible :</th> <td><?php echo \$logement->getAscenseuracc() ?></td>
                    </tr>
                    <tr>
                        <th>Logement adaptable :</th> <td><?php echo \$logement->getAdaptable() ?></td>
                    </tr>
                    <tr>
                        <th>Logement adapté :</th> <td><?php echo \$logement->getAdapte() ?></td>
                    </tr>
                </table>
            </div>

EOF;

    //create new PDF document (document units are set by default to millimeters)
    $pdf = new sfTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor(PDF_AUTHOR);
    $pdf->SetTitle($doc_title);

    //set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

    //set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 

    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    //set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    //set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // Fixe la taille de la page
    $pdf->SetDisplayMode(90);

    //initialize document
    $pdf->SetFont('helvetica', '', 10);
    $pdf->AddPage();

    // output some HTML code


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

    //reset pointer to the last page
    $pdf->lastPage();  

    // Close and output PDF document
    $pdf->Output('fichelogement.pdf', 'I');

    // Stop symfony process
    throw new sfStopException();


推荐答案

首先, HTML和不同格式,几乎没有什么共同点。如果TCPDF允许您使用HTML和CSS提供输入数据,因为它实现了这两种语言的简单解析器,并试图找出如何将其转换为PDF。所以这是合乎逻辑的,TCPDF只支持HTML和CSS规范的一个小的子集,即使在支持的东西,它可能不像一流的Web浏览器一样完美。

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.

查看源代码,我们可以看到有一个受保护的方法 TCPDF :: getHtmlDomArray(),除了别的以外,解析CSS声明。我可以看到像 font-family list-style-type 但根据我可以看到,没有保证金填充,最后,

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.

总而言之:使用TCPDF,您可以使用CSS来处理某些基本格式化。如果你需要从HTML转换为PDF,这是错误的工具。 (如果是这样,我可以建议 wkhtmltopdf ?)

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不渲染所有CSS属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆