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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35985904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:39:08  来源:igfitidea点击:

I want to set header and footer in PDF using mpdf

phphtmlcodeigniterpdf

提问by Saurabh Gujarani

I have generated a PDF using mpdflibrary in CodeIgniter. I want to attach header image and footer image with proper margin, I have created one code but header and footer get overlapped.

我已经使用mpdfCodeIgniter 中的库生成了一个 PDF 。我想以适当的边距附加页眉图像和页脚图像,我创建了一个代码,但页眉和页脚重叠。

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

在此处输入图片说明

回答by Saurabh Gujarani

 $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");

Addpage for to creating page in mpdf and pass parameters of margin top,bottom then we get proper output

添加页面以在 mpdf 中创建页面并传递边距顶部、底部的参数然后我们得到正确的输出

回答by Sameer Kumar Choudhary

You can set custom HTMl Header for first page and Other page as below: Just add the below snippet above all html content. You can add Inline CSS for it using tags above al html

您可以为第一页和其他页面设置自定义 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" />';

Or can use https://mpdf.github.io/headers-footers/headers-footers.htmlas refrence to use HTML header function

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

To Set footer one can use:

要设置页脚,可以使用:

// 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
];