如何在渲染pdf时为html2pdf指定页眉/页脚?

时间:2013-04-24 16:28:25

标签: python html2pdf

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

2 个答案:

答案 0 :(得分:4)

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

<?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;
    }

答案 1 :(得分:1)

简易蓝莓!您可以使用页面和框架来定义页眉和页脚,方法是将它们放在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