Codeigniter 在dompdf中添加带有页码的页眉和页脚

Codeigniter 在dompdf中添加带有页码的页眉和页脚,codeigniter,dompdf,Codeigniter,Dompdf,我正在与Codeigniter合作,我成功地实现了生成PDF文件的dompdf。现在我在生成的PDF中添加页眉和页脚时遇到了问题 以下是my dompdf\u帮助程序代码: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); function pdf_create($html, $filename='', $stream=TRUE) { require_once("dompdf/dompdf_c

我正在与Codeigniter合作,我成功地实现了生成PDF文件的dompdf。现在我在生成的PDF中添加页眉和页脚时遇到了问题

以下是my dompdf\u帮助程序代码:

<?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生成的my controller:

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

如果(isset($pdf)){
$font=font_度量:获取字体(“helvetica”、“bold”);
$pdf->page_text(500,10,“page:{page_NUM}(共{page_COUNT})”,$font,6,数组(0,0,0));
}

我想打印公司名称、联系方式和帐单号码,作为每页的页眉,然后作为页脚。我想添加一个页码,如“第1页,共3页”。

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


@第{页边距:180px 50px;}
#标题{位置:固定;左侧:0px;顶部:-180px;右侧:0px;高度:150px;背景色:橙色;文本对齐:中间;}
#页脚{位置:固定;左侧:0px;底部:-180px;右侧:0px;高度:150px;背景色:浅蓝色;}
#页脚。页码:{内容:计数器(页码,上罗马);}
Widgets Express
第页

第一页

第二页


我也曾尝试将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));
?>
require_once(“dompdf_config.inc.php”);
$html='1
你好你好

你好2

你好3

'; $dompdf=新的dompdf(); $dompdf->load_html($html); $dompdf->render(); $canvas=$dompd->get_canvas(); $font=font_度量:获取字体(“helvetica”、“bold”); $canvas->page_text(72,18,“标题:{page_COUNT}中的{page_NUM},$font,6,数组(0,0,0)); $dompdf->stream(“my_pdf.pdf”,数组(“附件”=>0)); ?>

/** 
将页边距设置为0,以便页脚和页眉
可以是全高全宽!
**/
@页面{
边距:0厘米0厘米;
}
/**现在定义PDF中每个页面的实际页边距**/
身体{
边缘顶部:4厘米;
左边距:2厘米;
右边距:2cm;
边缘底部:4厘米;
}
/**定义标题规则**/
标题{
位置:固定;
顶部:2厘米;
左:2厘米;
右:2厘米;
高度:2厘米;
/**超个人风格
背景色:#03a9f4;
颜色:白色;
**/
文本对齐:居中;
线高:1.5cm;
}
/**定义页脚规则**/
页脚{
位置:固定;
底部:0厘米;
左:0厘米;
右:0厘米;
高度:2厘米;
/**超个人风格**/
背景色:#03a9f4;
颜色:白色;
文本对齐:居中;
线高:1.5cm;
}
第1条。
投标人须知
版权及副本;

使用页眉-页脚标记

调试dompdf时,首先要开始查看正在呈现的HTML文档,而不是创建文档的过程。这样更容易识别与dompdf相关的错误。那么,您可以发布一个显示错误的示例文档吗?我唯一的问题是,您是否至少在HTML内容周围有body标记?如果不是,则由于文档的处理方式,dompdf将无法访问脚本。可能很明显,但请确保将常量dompdf\u ENABLE\u PHP设置为true。@tomvo-不太明显!谢谢你的提示。我也在尝试添加页码,但这对我不起作用。我尝试了'Page of'和'echo$Page_NUM;'他们都没有输出任何东西。我的选项中确实启用了php。有什么想法吗?同样的想法,同样的尝试和错误。不能让它工作。唯一的方法是CSS'
计数器(页面)
,但不能让
计数器(页面)
显示除
0
以外的内容。。。
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));
?>
<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>