Movatterモバイル変換


[0]ホーム

URL:


mPDF Manual – Headers & Footers

Method 2

This usesRUNTIMEHTML headers & footers. This is the simplest & quickest way to program a header/footer once for the whole document that includes images or uses more complex layout styles.

Setting Headers/Footers for the whole document

UseSetHTMLHeader() and/orSetHTMLFooter() to set HTML headers/footers before writing to the document.

Example #1 - Single-sided document

<?php$mpdf=new\Mpdf\Mpdf();// Define the Header/Footer before writing anything so they appear on the first page$mpdf->SetHTMLHeader('<div>    My document</div>');$mpdf->SetHTMLFooter('<table width="100%">    <tr>        <td width="33%">{DATE j-m-Y}</td>        <td width="33%" align="center">{PAGENO}/{nbpg}</td>        <td width="33%">My document</td>    </tr></table>');$mpdf->WriteHTML('Hello World');$mpdf->Output();

Example #2 - Double-sided document

<?php$mpdf=new\Mpdf\Mpdf();// Define the Headers before writing anything so they appear on the first page$mpdf->SetHTMLHeader('<div>My document</div>','O');$mpdf->SetHTMLHeader('<div>My document</div>','E');$mpdf->SetHTMLFooter('<table width="100%">    <tr>        <td width="33%">{DATE j-m-Y}</td>        <td width="33%" align="center">{PAGENO}/{nbpg}</td>        <td width="33%">My document</td>    </tr></table>');// Note that the second parameter is optional : default = 'O' for ODD$mpdf->SetHTMLFooter('<table width="100%">    <tr>        <td width="33%"><span>My document</span></td>        <td width="33%" align="center">{PAGENO}/{nbpg}</td>        <td width="33%">{DATE j-m-Y}</td>    </tr></table>','E');$mpdf->WriteHTML('Hello World');$mpdf->Output();

Changing Header/Footer during the document

This is whereRUNTIME headers/footers get much more clumsy to use. When a new page is added to the document (e.g. usingAddPage() or <pagebreak>) mPDF does the following:

  • writes the footer for the current page
  • starts the new page
  • writes the header for the new page

Therefore to use anyRUNTIME method you need to:

  • change the header before the page-break
  • change the footer after the page-break

Example #1

<?php// First ensure that you are on an Even page$mpdf->AddPage('','E');// Then set the headers for the next page before you add the page$mpdf->SetHTMLHeader('<div>    Chapter 2</div>','O');$mpdf->SetHTMLHeader('<div>    Chapter 2</div>','E');$mpdf->AddPage();$mpdf->SetHTMLFooter('<div>    Chapter 2</div>','O');$mpdf->SetHTMLFooter('<div>    Chapter 2</div>','E');$mpdf->WriteHTML('Rest of the document');$mpdf->Output();

Example #2 - Turning a Header/Footer off

<?php// If you want the changes to start on an ODD page$mpdf->AddPage('','E');$mpdf->SetHTMLHeader();$mpdf->AddPage();$mpdf->SetHTMLFooter();$mpdf->WriteHTML('No-Header page');$mpdf->Output();

Table of Contents

UsingRUNTIME headers/footers with a Table of Contents is very clumsy, it is strongly recommended that you use one of theNAMED methods. Here for the record is how you would do it:

<?php$mpdf=new\Mpdf\Mpdf();// Set the headers/footers for the Introduction$mpdf->SetHTMLHeader('<div>    Introduction</div>','O');$mpdf->SetHTMLHeader('<div>    Introduction</div>','E');$mpdf->SetHTMLFooter('<div>{PAGENO}</div>','O');$mpdf->SetHTMLFooter('<div>{PAGENO}</div>','E');$mpdf->AddPage('','',1,'','on');// suppress page numbering for the introduction$mpdf->WriteHTML('Introduction of document...');$mpdf->AddPage('','E');$mpdf->SetHTMLHeader('<div>Main</div>','O');$mpdf->SetHTMLHeader('<div>Main</div>','E');$mpdf->TOCpagebreak('','','','','','','','','','','','','','','','','',$toc-preHTML,$toc-postHTML,$toc-bookmarkText,1,'A','off');// sets numbering to start at A$mpdf->SetHTMLFooter('<div>Main - {PAGENO}</div>','O');$mpdf->SetHTMLFooter('<div>Main - {PAGENO}</div>','E');$mpdf->WriteHTML('Main part of document...');$mpdf->Output();

…and for historical reference, using deprecated TOC function:

<?php$mpdf=new\Mpdf\Mpdf();// Set the headers/footers for the Introduction$mpdf->SetHTMLHeader('<div>        Introduction    </div>','O');$mpdf->SetHTMLHeader('<div>        Introduction    </div>','E');$mpdf->SetHTMLFooter('<div>{PAGENO}</div>','O');$mpdf->SetHTMLFooter('<div>{PAGENO}</div>','E');$mpdf->AddPage('','',1,'','on');// suppress page numbering for the introduction$mpdf->WriteHTML('Introduction of document...');$mpdf->AddPage('','E');$mpdf->SetHTMLHeader('<div>Main</div>','O');$mpdf->SetHTMLHeader('<div>Main</div>','E');$mpdf->AddPage('','',1,'i','off');// sets page numbering to start here at 1$mpdf->SetHTMLFooter('<div>Main - {PAGENO}</div>','O');$mpdf->SetHTMLFooter('<div>Main - {PAGENO}</div>','E');// Set some variables for the ToC - these are all now deprecated$mpdf->TOCheader=array();$mpdf->TOCfooter=array();$mpdf->TOCpreHTML='<h2>Table of Contents</h2>');$mpdf->TOCpostHTML='Text to come after the contenst list';$mpdf->TOCbookmarkText='Contents';// Mark this current page as where the ToC is to be inserted$mpdf->TOC($tocfont,$tocfontsize,$tocindent,$resetpagenum,$pagenumstyle,$suppress,$toc_orientation,$TOCusePaging,$TOCuseLinking);$mpdf->WriteHTML('Main part of document...');$mpdf->Output();

See Also

Related to RUNTIME HTML headers & footers:


[8]ページ先頭

©2009-2026 Movatter.jp