DefHTMLHeaderByName 似乎不起作用 mpdf

DefHTMLHeaderByName doesn't seem to work mpdf

我正在尝试获取在 php 站点上生成的文档。一切正常,但最后一件事是在标题页之后的每一页顶部添加一个 header。我已经尝试了很多东西,最接近我想要的是以下(精简)代码。

  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,但无论哪种情况,我都得到了边距,除了 header 之外的所有内容都没有显示。为什么?为什么没有分配它以便我可以在 AddPageByArray 中调用它?

要显示 header,您需要将 odd-header-value 选项添加到具有值 1AddPageByArray 方法。

  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(安装作曲家,如果你没有)。
  4. 将此代码粘贴到 index.php

<?php
require_once __DIR__ . '/vendor/autoload.php';

ob_start();
$mpdf = new \Mpdf\Mpdf();


/** same code from previous example here, except Mpdf init **/

尝试这样的事情

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

可能对你有帮助。

Url: https://mpdf.github.io/headers-footers/headers-footers.html

根据文档,您需要在 header/footer 名称前加上“html_

尝试改变;

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

至;

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

引用here