在dompdf中添加页码的页眉和页脚
我使用Codeigniter,我成功地实现了dompdf生成PDF文件。现在我有问题,在生成的PDF中添加页眉和页脚。
I am working with Codeigniter and I successfully implemented dompdf for generating PDF files. Now I have issues on adding a header and footer in generated PDF.
这是我的dompdf_helper代码:
Here is the my dompdf_helper code:
<?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生成:
Here is the my controller to call PDF generation:
<?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);
?>
我使用这个脚本获取页码,但只有在第二页退出时才打印, t print。
I use this script for getting page number but it printed only if second page is exits otherwise it won't print.
<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/3。
I want to print the company name and contact details and bill number as header in every page then in a footer. I want to add a page number like "1 of 3".
我希望这有助于您理解如何在dompdf中获取页眉和页脚。也检查此链接.... 示例
I hope this will help you lot in understanding how to get header and footer in dompdf. Check this link also....Example
<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>