我想使用mpdf在pdf中设置页眉和页脚

时间:2016-03-14 11:09:58

标签: php html codeigniter pdf

我使用PDF中的mpdf库生成一个Codeigniter。 我想以适当的边距附加标题图像和页脚图像,我创建了一个代码,但页眉和页脚重叠请帮我解决这个问题 提前谢谢

//controller
$this->load->library('m_pdf');
    $param = '"","A4","","",0,0,100,0,6,3,"L"';
    $pdf = $this->m_pdf->load($param);
    // $pdf->useOddEven = true;

    $pdf->SetHTMLHeader('<img src="' . base_url() . 'custom/Hederinvoice.jpg"/>');

    $pdf->SetHTMLFooter('<img src="' . base_url() . 'custom/footarinvoice.jpg"/>');
    $wm = base_url() . 'custom/Watermark.jpg';
    $pdf->SetWatermarkImage($wm);
    $pdf->showWatermarkImage = true;
      $data['main_content'] = 'dwnld';
    $this->load->view('template', $data);
    $html = $this->load->view('template_pdf', $data, true);
    $this->load->view('template_pdf', $data, true);
    $pdf->WriteHTML($html);
    $pdf->page = 0;
    $pdf->state = 0;
    $pdf->Output($pdfFilePath, "D");

enter image description here

2 个答案:

答案 0 :(得分:10)

 $pdf->SetHTMLHeader('<img src="' . base_url() . 'custom/Hederinvoice.jpg"/>');

    $pdf->SetHTMLFooter('<img src="' . base_url() . 'custom/footarinvoice.jpg"/>');
    $wm = base_url() . 'custom/Watermark.png';

      $data['main_content'] = 'dwnld';
    //$this->load->view('template', $data);
    $html = $this->load->view('template_pdf', $data, true);
    $this->load->view('template_pdf', $data, true);
    $pdf->AddPage('', // L - landscape, P - portrait 
        '', '', '', '',
        5, // margin_left
        5, // margin right
       60, // margin top
       30, // margin bottom
        0, // margin header
        0); // margin footer
    $pdf->WriteHTML($html);

    $pdf->Output($pdfFilePath, "D");

为在mpdf中创建页面添加页面并传递margin top,bottom的参数然后我们得到正确的输出

答案 1 :(得分:0)

您可以为首页和其他页面设置自定义HTMl标头,如下所示:  只需在所有html内容上方添加以下代码段即可。您可以使用al html上方的标签为其添加内联CSS

              <htmlpageheader name="firstpage" style="display:none">
              <div style="text-align:center"><img src="'.'logo.png"/></div>
              </htmlpageheader><sethtmlpageheader name="firstpage" value="on" show-this-page="1" />
              <htmlpageheader name="otherpages" style="display:none">
              <div style="text-align:center">{PAGENO}</div>
              </htmlpageheader><sethtmlpageheader name="otherpages" value="off" />';

或者可以使用https://mpdf.github.io/headers-footers/headers-footers.html作为参考以使用HTML标头功能

要设置页脚,可以使用:

// Footer Configuration for all pages, can be changed for odd and even pages accordingly
$oddEvenConfiguration = 
            [
                'L' => [ // L for Left part of the header
                'content' => 'Your Content',
                'font-size' => 10,
                'font-family' => 'sans-serif',
                'color'=>'#000000'
                ],
                'C' => [ // C for Center part of the header
                'content' => '',
                ],
                'R' => [
                    'content' => '{PAGENO}',
                ],
                'line' => 0, // That's the relevant parameter to remove line style in footer
            ];

$headerFooterConfiguration = [
    'odd' => $oddEvenConfiguration,
    'even' => $oddEvenConfiguration
];