Search code examples
phpmpdf

DefHTMLHeaderByName doesn't seem to work mpdf


I am trying to get a document generated on a php site. It is all working well but the last thing is to add a header to the top of every page after the title page. I have tried so many things and the closest I can get to what I want is the following (stripped down) code.

  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();

I have tried changing the odd-header-name to html_PgHeader based on the documentation but in either case, I get the margin, all the content but the header never shows up. Why? Why is it not being assigned so I can call it in the AddPageByArray?


Solution

  • To show header, you need to add odd-header-value option to AddPageByArray method with value 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

    If this, don't help, then try this scenario:

    1. Open console and go to site's root path.
    2. Create index.php file
    3. Run composer require mpdf/mpdf (install composer, if you don't have it).
    4. Paste this code to the index.php

    <?php
    require_once __DIR__ . '/vendor/autoload.php';
    
    ob_start();
    $mpdf = new \Mpdf\Mpdf();
    
    
    /** same code from previous example here, except Mpdf init **/