【问题标题】:How do I specify a header/footer for html2pdf to use when rendering a pdf?如何在呈现 pdf 时为 html2pdf 指定页眉/页脚?
【发布时间】:2013-04-24 16:28:24
【问题描述】:

我正在使用 html2pdf python 库,并且想定义一个页眉和一个页脚以应用于每个页面(包括有趣的东西,比如页脚的页数)。我可以使用 html2pdf 指定页眉/页脚的最方便的方法是什么?

【问题讨论】:

    标签: python html2pdf


    【解决方案1】:

    看看这是否需要朋友。页眉和页脚是固定的并通知页数。

    <?php
    /**
     * HTML2PDF Librairy - example
     *
     * HTML => PDF convertor
     * distributed under the LGPL License
     *
     * @author      Laurent MINGUET <webmaster@html2pdf.fr>
     *
     * isset($_GET['vuehtml']) is not mandatory
     * it allow to display the result in the HTML format
     */
    
    ob_start();
    
    // HTML template begin (no output)
    
    ?>
    <style type="text/css">
    <!--
        table.page_header {width: 100%; border: none; background-color: #DDDDFF; border-bottom: solid 1mm #AAAADD; padding: 2mm }
        table.page_footer {width: 100%; border: none; background-color: #DDDDFF; border-top: solid 1mm #AAAADD; padding: 2mm}
    
        div.niveau
        {
            padding-left: 5mm;
        }
    -->
    </style>
    <page backtop="14mm" backbottom="14mm" backleft="10mm" backright="10mm" style="font-size: 12pt">
        <page_header>
            <table class="page_header">
                <tr>
                    <td style="width: 100%; text-align: left;">
                        Exemple d'utilisation des bookmarks
                    </td>
                </tr>
            </table>
        </page_header>
        <page_footer>
            <table class="page_footer">
                <tr>
                    <td style="width: 100%; text-align: right">
                        page [[page_cu]]/[[page_nb]]
                    </td>
                </tr>
            </table>
        </page_footer>
    </page>
    <?php
    
        // HTML end 
        // Getting the html which was not displayed into $content var
        $content = ob_get_clean();
    
        require_once(dirname(__FILE__).'/../html2pdf.class.php');
        try
        {
            $html2pdf = new HTML2PDF('P', 'A4', 'fr', true, 'UTF-8', 0);
            $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
            $html2pdf->createIndex('Sommaire', 25, 12, false, true, 1);
            $html2pdf->Output('bookmark.pdf');
        }
        catch(HTML2PDF_exception $e) {
            echo $e;
            exit;
        }
    

    【讨论】:

      【解决方案2】:

      简单的蓝莓!您可以使用页面和框架来定义页眉和页脚,方法是将它们放在 HTML 文档的样式标签中。

      <html>
          <head>
          <style>
          /* Page margins are defined using CSS */
          @page {
            margin: 1cm;
            margin-top:2.5cm;
            margin-bottom: 2.5cm;
      
          /* Header frame starts within margin-top of @page */
            @frame header {
            -pdf-frame-content: headerContent; /* headerContent is the #id of the element */
            top: 1cm;
            margin-left: 1cm;
            margin-right:1cm;
            height:1cm;
            }
      
          /* Footer frame starts outside margin-bottom of @page */
            @frame footer {
              -pdf-frame-content: footerContent;
              bottom: 2cm;
              margin-left: 1cm;
              margin-right: 1cm;
              height: 1cm;
            }
          }
          </style>
          </head>
          <body>
                 <div id="headerContent">I'm a header!</div>
                 <p>I could be some content</p>
                 <div id="footerContent">I'm a footer! <pdf:pagenumber></div>
          </body>
      </html>
      

      pdf:pagenumber 是用于显示页数的标签。包括更多标签。参考官方文档即可!

      来源HTML2PDF Github Documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-25
        • 2016-11-03
        • 2017-04-21
        • 1970-01-01
        • 2014-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多