4

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

4

2 回答 2

7

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

<?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;
    }
于 2014-07-10T12:18:33.010 回答
3

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

于 2013-05-07T06:20:40.303 回答