7

我正在使用 Codeigniter,并成功实现了 dompdf 来生成 PDF 文件。现在我在生成的 PDF 中添加页眉和页脚时遇到问题。

这是我的 dompdf_helper 代码:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename='', $stream=TRUE) 
{
require_once("dompdf/dompdf_config.inc.php");

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
  $dompdf->set_paper("A4");
if ($stream) {
    $dompdf->stream($filename.".pdf",1);
} else {
    return $dompdf->output();
}
}
?>

这是我调用 PDF 生成的控制器:

<?php

$data['store']=$res;  
$this->load->helper(array('dompdf', 'file'));
$html = $this->load->view('store/sales_pdf', $data, true);
$html.= $this->load->view('footer');
$filename="salesbill".$id;
pdf_create($html, $filename);
$data = pdf_create($html, '', false);
write_file('name', $data); 
?>

我使用此脚本获取页码,但仅在退出第二页时才打印,否则不会打印。

  <script type="text/php">

    if ( isset($pdf) ) {

      $font = Font_Metrics::get_font("helvetica", "bold");
      $pdf->page_text(500,10, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));

    }
    </script>

我想在每一页中打印公司名称和联系方式以及账单号作为页眉,然后在页脚中。我想添加一个像“1 of 3”这样的页码。

4

3 回答 3

22

我希望这将有助于您理解如何在 dompdf 中获取页眉和页脚。也检查此链接....示例

<html>
  <head>
   <style>
     @page { margin: 180px 50px; }
     #header { position: fixed; left: 0px; top: -180px; right: 0px; height: 150px; background-color: orange; text-align: center; }
     #footer { position: fixed; left: 0px; bottom: -180px; right: 0px; height: 150px; background-color: lightblue; }
     #footer .page:after { content: counter(page, upper-roman); }
   </style>
  <body>
   <div id="header">
     <h1>Widgets Express</h1>
   </div>
   <div id="footer">
     <p class="page">Page <?php $PAGE_NUM ?></p>
   </div>
   <div id="content">
     <p>the first page</p>
     <p style="page-break-before: always;">the second page</p>
   </div>
 </body>
 </html>
于 2012-10-18T11:18:21.950 回答
12

我也尝试将 PHP 代码添加到 html 中,但从未有机会使其工作。这是我保证有效的完整内联代​​码:

require_once("dompdf_config.inc.php");
$html ='<html>
        <body>
        <p>Hello Hello</p><p style="page-break-after:always;page-break-before:always">Hello Hello 2</p><p>Hello Hello 3</p>
        </body>
        </html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);

$dompdf->render();

$canvas = $dompdf->get_canvas();
$font = Font_Metrics::get_font("helvetica", "bold");
$canvas->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));

$dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
?>
于 2013-02-19T15:28:21.340 回答
0
<style>
        /** 
            Set the margins of the page to 0, so the footer and the header
            can be of the full height and width !
         **/
        @page {
            margin: 0cm 0cm;
        }

        /** Define now the real margins of every page in the PDF **/
        body {
            margin-top: 4cm;
            margin-left: 2cm;
            margin-right: 2cm;
            margin-bottom: 4cm;
        }

        /** Define the header rules **/
        header {
            position: fixed;
            top: 2cm;
            left: 2cm;
            right: 2cm;
            height: 2cm;

            /** Extra personal styles 
            background-color: #03a9f4;
            color: white;
            **/
            text-align: center;
            line-height: 1.5cm;
        }

        /** Define the footer rules **/
        footer {
            position: fixed; 
            bottom: 0cm; 
            left: 0cm; 
            right: 0cm;
            height: 2cm;

            /** Extra personal styles **/
            background-color: #03a9f4;
            color: white;
            text-align: center;
            line-height: 1.5cm;
        }
    </style>
    <header>
        <table  class="table table-bordered" border='1' width='100%' style='font-size:16px; border-collapse: collapse;/* border-collapse: separate; border-spacing: 30px;*/'>
            <tr> 
                <th scope="col" style="padding:10px;" width="10%">Clause No.</th> 
                <th scope="col" style="padding:10px;" width="80%">INSTRUCTIONS TO BIDDERS</th> 
            </tr>
        </table>
    </header>
    <?php endif;?>
    <footer>
        Copyright &copy; <?php echo date("Y");?> 
    </footer>

使用页眉页脚标签

于 2019-11-22T05:26:20.693 回答