DefHTMLHeaderByName似乎没有工作mpdf

时间:2018-06-08 14:15:18

标签: php mpdf

我试图在php网站上生成一个文档。这一切都运作良好,但最后一件事是在标题页后面的每个页面顶部添加一个标题。我已经尝试了很多东西,最接近我想要的是以下(剥离)代码。

  ob_start();

  $mpdf = new Mpdf();

  $mpdf->SetDisplayMode('fullpage');
  $mpdf->bleedMargin = 0;
  $mdpf->crossMarkMargin = 0;
  $mpdf->cropMarkMargin = 0;
  $mpdf->nonPrintMargin = 0;
  // Home page and headers...
  $html_header = 'Some Code here';
  $mpdf->DefHTMLHeaderByName('PgHeader', $html_header);

  $mpdf->AddPage();
  $mpdf->Image('cover.jpg', 0, 0, 210, 297, 'jpg', '', true, false);


    // Get the content of any pages we have 
    if($pdf_pages) {
      foreach($pdf_pages as $key=>$pg) {
        $mpdf->AddPageByArray(array(
          'mgt' => '40',
          'odd-header-name' => 'PgHeader'
        ));
        $html .= 'the content from the loop';

          // Write some HTML code:
          $custom_sheet = file_get_contents('/custom.css');

          $mpdf->WriteHTML($custom_sheet, 1);
          $mpdf->WriteHTML($html, 2);
      }
    }

  // Output a PDF file directly to the browser
  $mpdf->Output('Name.pdf', 'I');

  ob_end_flush();

我已经尝试根据文档将odd-header-name更改为html_PgHeader但在任何一种情况下,我都获得了保证金,所有内容但标题永远不会出现。为什么?为什么没有分配,所以我可以在AddPageByArray中调用它?

3 个答案:

答案 0 :(得分:1)

根据文档,您需要在页眉/页脚名称前添加' html _ '

尝试更改;

'odd-header-name' => 'PgHeader',

至;

'odd-header-name' => 'html_PgHeader',

参考here

答案 1 :(得分:0)

要显示标题,您需要向odd-header-value方法添加AddPageByArray选项,其值为1

  ob_start();

  $mpdf = new Mpdf();

  $mpdf->SetDisplayMode('fullpage');
  $mpdf->bleedMargin = 0;
  $mdpf->crossMarkMargin = 0;
  $mpdf->cropMarkMargin = 0;
  $mpdf->nonPrintMargin = 0;
  // Home page and headers...
  $html_header = 'Some Code here';
  $mpdf->DefHTMLHeaderByName('PgHeader', $html_header);

  $mpdf->AddPage();
  $mpdf->Image('cover.jpg', 0, 0, 210, 297, 'jpg', '', true, false);

  $pdf_pages = array('key' => 'val');

    // Get the content of any pages we have 
    if($pdf_pages) {
      foreach($pdf_pages as $key=>$pg) {
        $mpdf->AddPageByArray(array(
          'mgt' => '40',
          'odd-header-name' => 'PgHeader',
          'odd-header-value' => 1,
        ));
        $html .= 'the content from the loop';

          // Write some HTML code:
          $custom_sheet = file_get_contents('/custom.css');

          $mpdf->WriteHTML($custom_sheet, 1);
          $mpdf->WriteHTML($html, 2);
      }
    }

  // Output a PDF file directly to the browser
  $mpdf->Output('Name.pdf', 'I');

  ob_end_flush();

<强> Pt.II

如果这样,请不要帮助,然后尝试这种情况:

  1. 打开控制台并转到网站的根路径。
  2. 创建index.php文件
  3. 运行composer require mpdf/mpdf(如果没有,请安装composer)。
  4. 将此代码粘贴到index.php
  5. <?php
    require_once __DIR__ . '/vendor/autoload.php';
    
    ob_start();
    $mpdf = new \Mpdf\Mpdf();
    
    
    /** same code from previous example here, except Mpdf init **/
    

答案 2 :(得分:-1)

尝试这样的事情

@page {
 header: header_name;
 footer: footer_name;
}

它可能对你有帮助。

网址:https://mpdf.github.io/headers-footers/headers-footers.html

相关问题