Deprecated since mPDF ≥ 6
non-HTML is now handled as HTML as well
This usesNAMEDNON-HTML headers & footers.This method is useful if you do not need the flexibility of an HTML header/footer, but are changing headers/footersthroughout the document.
These use the same array values asMethod 1Variant #5.
Note that named headers are not specified asODD orEVENwhen they are defined, but only when they are selected.
Note: Do not name a header or footer starting withhtml_ - This prefix is reserved to identify anHTML header/footer.
<?php$arr1=array('L'=>array('content'=>'Chapter 1','font-size'=>10,'font-style'=>'B','font-family'=>'serif','color'=>'#000000'),'C'=>array('content'=>'','font-size'=>10,'font-style'=>'B','font-family'=>'serif','color'=>'#000000'),'R'=>array('content'=>'My document','font-size'=>10,'font-style'=>'B','font-family'=>'serif','color'=>'#000000'),'line'=>1,);$mpdf->DefHeaderByName('MyHeader1',$arr1);<?php$mpdf=new\Mpdf\Mpdf();// Define the Headers and Footers with names$html='<pagefooter name="MyFooter1" content-left="{DATE j-m-Y}" content-center="{PAGENO}/{nbpg}" content-right="My document" footer-style="font-family: serif; font-size: 8pt; font-weight: bold; font-style: italic; color: #000000;" /><div>Now starts the document text... </div>';$mpdf->WriteHTML($html);Once you have definedNAMED headers/footers for your document, you can reference them using:
Methods forNON-HTML headers/footers only -Recommended when setting thefirst page header/footer at the start of a document (although they can also be used to change headers/footersduring the document).
Methods to access any headers/footers (HTML orNON-HTML) -Recommended when changing header/footer during the document.
When using aNAMED header on the first page, remember that mPDF writes the header asthe first page is started. This is usually when you first useWriteHTML() which automatically triggers an AddPage().
<?php$mpdf=new\Mpdf\Mpdf();// Define a header named 'MyHeader1' here (as Example #1)$mpdf->SetHeaderByName('MyHeader1');$mpdf->WriteHTML('Document text');$mpdf->Output();In this example using custom HTML tags to set theNON-HTML header, notice that<setpageheader> hasshow-this-page = "1". This is because as soon as you callWriteHTML(), mPDF has added the first page, so this fixes the problem by forcing the header to show on the first page:
<?php$mpdf=new\Mpdf\Mpdf();$html='<pagefooter name="MyFooter1" content-left="{DATE j-m-Y}" content-center="{PAGENO}/{nbpg}" content-right="My document" footer-style="font-family: serif; font-size: 8pt; font-weight: bold; font-style: italic; color: #000000;" /><setpageheader name="MyHeader1" value="on" show-this-page="1" /><setpagefooter name="MyFooter1" value="on" /><div>Start of the document ... and all the rest</div>';$mpdf->WriteHTML($html);$mpdf->Output();<?php$mpdf=new\Mpdf\Mpdf();$html='<html><head><style> @page { size: auto; odd-header-name: MyHeader1; odd-footer-name: MyFooter1; } @page chapter2 { odd-header-name: MyHeader2; odd-footer-name: MyFooter2; } @page noheader { odd-header-name: _blank; odd-footer-name: _blank; } div.chapter2 { page-break-before: always; page: chapter2; } div.noheader { page-break-before: always; page: noheader; }</style></head><body> <pageheader name="MyHeader1" content-right="My document" header-style="font-weight: bold; color: #000000;" line="on" /> <pagefooter name="MyFooter1" content-left="{DATE j-m-Y}" content-center="{PAGENO}/{nbpg}" footer-style="font-size: 8pt;" /> <pageheader name="MyHeader2" content-right="Chapter 2" header-style="font-weight: bold; color: #000000;" line="on" /> <pagefooter name="MyFooter2" content-left="{DATE j-m-Y}" content-center="2: {PAGENO}" footer-style="font-size: 8pt;" /> <div>Here is the text of the first chapter</div> <div>Text of Chapter 2</div> <div>No-Header page</div></body></html>';$mpdf->WriteHTML($html);$mpdf->Output();<?php$mpdf->WriteHTML('Document text');// In a SINGLE-SIDED document, the 'ODD' values set the default for all pages.$mpdf->AddPage('','','','','','','','','','','','MyHeader2','','MyFooter2','',1,0,1,0);$mpdf->WriteHTML('Document text');// Turn Headers and Footers off$mpdf->AddPage('','','','','','','','','','','','','','','',-1,0,-1,0);$mpdf->WriteHTML('Document text with No Headers/Footers');<?php$html='<p>Document text</p><p>Text of Chapter 2</p><!-- TO TURN HEADER/FOOTER OFF FOR A NEW PAGE --><pagebreak odd-header-value="off" odd-footer-value="off" /><p>No-Header page</p>';$mpdf->WriteHTML($html);<?php$mpdf=new\Mpdf\Mpdf();// Define headers here named 'MyHeader1', 'MyTOCHeader', 'MyTOCFooter',// 'MyHeader2', 'MyFooter2' (as Example #1)$mpdf->SetHeaderByName('MyHeader1');$mpdf->WriteHTML('Introduction of document...');$mpdf->TOCpagebreak('','','','','','','','','','','','','MyTOCHeader','','MyTOCFooter','',1,0,1,0,'','','','','','','','','','','','','','MyHeader2','','MyFooter2','',1,0,1,0);$mpdf->WriteHTML('Main part of document...');$mpdf->Output();<?php$html="<!-- Define headers etc. here named 'MyHeader1', 'MyTOCHeader', 'MyTOCFooter','MyHeader2', 'MyFooter2' (as Example #2) --><p>Introduction: Here starts the document</p><tocpagebreak toc-odd-header-name='MyTOCHeader' toc-odd-header-value=\"1\" toc-odd-footer-name='MyTOCFooter' toc-odd-footer-value=\"1\" odd-header-name='MyHeader2' odd-header-value=\"1\" odd-footer-name='MyFooter2' odd-footer-value=\"1\" /><p>Text of Chapter 2...</p>";$mpdf->WriteHTML($html);Related to NAMED non-HTML headers & footers: