代码之家  ›  专栏  ›  技术社区  ›  Elorfin

TCPDF不能呈现所有CSS属性

  •  35
  • Elorfin  · 技术社区  · 14 年前

    我给你代码(对不起,有点长…)

    $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();
    
    10 回复  |  直到 11 年前
        1
  •  79
  •   Stan James nfaggian    9 年前

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

    话虽如此,问题是:支持什么,不支持什么?文档基本上跳过了这个问题,让您享受试错法。

    看看源代码,我们可以看到有一个受保护的方法叫做 TCPDF::getHtmlDomArray() 除其他外,它解析CSS声明。我能看到像这样的东西 font-family , list-style-type text-indent margin padding 在我看来,肯定没有 float

    总而言之:有了TCPDF,您可以使用CSS来完成一些任务 基本的 格式化。如果您需要从HTML转换为PDF,它是错误的工具(如果是这样的话,我可以建议吗 wkhtmltopdf ?)

        2
  •  11
  •   DarthRez    10 年前

        3
  •  10
  •   KotaKomputer    14 年前

    TCPDF 5.9.010(2010-10-27) -添加了对CSS属性“border spacing”和“padding”的支持。

        4
  •  9
  •   takeshin    12 年前

    下面的代码示例

    $table = '<table width="100%" cellspacing="0" cellpadding="55%">
              <tr valign="bottom">
                    <td class="header1" rowspan="2" align="center" valign="middle"
                          width="6%">Category</td>
                    <td class="header1" rowspan="2" align="center" valign="middle"
                          width="26%">Project Description</td>
              </tr></table>';
    
        5
  •  6
  •   William Turrell    9 年前

    有些东西包含在 <style> 标签,但是如果添加到 style=""

    table {
        padding: 5px;
    }
    

    这个 :

    <table style="padding: 5px;">
    
        6
  •  5
  •   Jonathas Pacífico    11 年前

    在我写这篇文章的时候,TCPDF只支持填充表。

    Link to their forum page with that information

        7
  •  4
  •   user412934    14 年前

    由于版本5.7,TCPDF包含对CSS边框的支持。 尚不支持边距、填充和浮动。 请访问TCPDF网站 http://www.tcpdf.org 更多信息请咨询官方论坛。

        8
  •  3
  •   Joe    14 年前

    我使用表格来实现填充布局,因此为了创建一个具有内部填充的div,我创建了一个具有3列/3行的表格,并将内容放在中间的行/列中。第一列和最后一列/行用于填充。

    如。

    <table>
    <tr>
        <td width="10">&nbsp;</td>
        <td>&nbsp;</td>
        <td width="10">&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>content goes here</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td width="10">&nbsp;</td>
        <td>&nbsp;</td>
        <td width="10">&nbsp;</td>
    </tr>
    </table>
    

    希望有帮助。

        9
  •  2
  •   TRiG    5 年前

    我发现这个:

    // 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);
    

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

        10
  •  1
  •   Alexander Fedorov    9 年前

    只是一个小提示设置自定义填充没有额外的表元素。

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