How to get the total number of pages in MPDF? - php

I'm using PHP MPDF to create PDF files. At the footer I'm able to print the current page number using
{PAGENO}
But how do I get the total number of pages in the PDF file? So that in the footer I can print like:
Page (Current Page) of (Total number of pages)
Page 1 of 6

According to the official documentation:
$mpdf=new mPDF();
$mpdf->setFooter("Page {PAGENO} of {nb}");

you can use {nb} it gives you total number of pages in mpdf

I use the following in my html header template :
{PAGENO}/{nb}
For example,on 1st of total of 3 pages, the result will be :
1/3
So, you can write it wherever you want in your HTML code like header, footer, body...

Related

mPDF pagination problem when using WriteHTML function

I have a dinamically generated HTML content and want to generate a page-numbered PDF using mPDF. The problem is that when the content spans more than one page, the footer is only visible in the last page.
Example:
$mpdf->AliasNbPages('{PAGETOTAL}');
$mpdf->WriteHTML($content); // $content is dynamic, it can be a series of paragraphs resulting in a 3 or 4, or more, pages long pdf.
$mpdf->setFooter('{PAGENO}/{PAGETOTAL}'); // this only prints a footer in the last page, displaying for example "3/3", but the previous pages do not have footer
I want the footer in every page, but i don't know in advance how many pages I will have or where the pagebreaks will be.
Am I doing something wrong?
Try putting setFooter before WriteHTML
So, this way:
$mpdf->setFooter('{PAGENO}/{PAGETOTAL}');
$mpdf->WriteHTML($content);

Create dynamic Header that is aware of the amount of PDFs created in one 'addPage` operation

I have an application that prints several dynamic HTML Pages to PDF.
Usually
foreach($html as $htm) {
$pdf->AddPage();
$pdf->writeHTML($htm,true,0,true,0);
}
Every html Page starts with an Header, which it takes directly from the HTML Template, which denotes the number of the page, i.e. Page 1 for the first page, Page 2 for the second ...
Usually, one HTML Page consists of exactly one PDF, but sometimes the number of HTML Pages exceeds.
In this case, I want the header to print Page 1 of 1 for the first subpage of the first page, Page 2 of 2 for the second one....
The problem is, it seems, that I cannot use the header of the Template Page since it must now whether there will be several PDFs printed from it.
How do I solve that?
The getAliasNbPages method returns a token that TCPDF later updates with the actual number of pages.
Similarly, the getAliasNumPage method will return an alias string for the current page number.
For example, your Header could contain something like:
$pdf->writeHTML('Page '. $pdf->getAliasNumPage() . ' of ' . $pdf->getAliasNbPages());

How to insert mpdf TOC inside first page?

I am working on PDF generation using the MPDF library into a Drupal 8 project.
I am planning to put the table of contents (TOC) inside a <div> on the first page of my PDF.
For that, I created a <tocentry> tags and put the <tocpagebreak /> into my <div> of the first HTML page.
Unfortunately, the TOC is generated in a new page (i.e., page break is happening before and after the TOC).
How can I generate the TOC within my custom HTML structure and include it into my <div>?
By default the TOC goes at the bottom, but you can modify its position manually.
In mPDF's docs there's a nice example in which the TOC is before the content:
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Introduction');
$mpdf->TOCpagebreak();
$mpdf->TOC_Entry("Chapter 1", 0);
$mpdf->WriteHTML('Chapter 1 ...');
$mpdf->Output();
Notice that TOCpagebreak() is invoked before TOC_Entry (the function that adds the entry to the TOC) and before WriteHTML('Chapter 1 ...'). This latter function adds content to the PDF.
By placing TOCpagebreak() before those functions, you make the TOC appear before the contents.
For more details, see mPDF docs on TOCpagebreak()

MPDF - change background for last page?

#page :last {
background: rgb(167,0,51);
}
I need to add background for last page of my PDF but it not working. Only first page is getting bg but not last. Is there any solution?
Reference Link is given below.
MPDF - different background for first page
There is no option to add specific CSS for the Last page of PDF in MPDF till now.
But MDF provides options to add CSS to a specific page using "#page ".
(https://mpdf.github.io/css-stylesheets/supported-css.html)
If you are using AddPage() to create pages, you can just add a page selector(Example: "lastpage") to that specific page and add CSS like below.
#page lastpage{background: rgb(167,0,51);}
If you are using to create pages, you can add page selector (Example: < pagebreak page-selector="lastpage">) to that tag and add CSS like below.
#page lastpage{background: rgb(167,0,51);}
This will definitely work. I have tried and tested.

PHP Change Page width on Second Page (mPDF)

can anyone tell me on how could I change the page size on the second (2nd) page using mpdf. My page size is A4 (by mpdf default).
I want to dynamically changed the width of second page of my report since it's dynamic content of table (e.g. number of columns is undefined)
If you know where your first page ends, you can use the AddPage() function to change the formatting of the next page.
$mpdf=new mPDF();
$mpdf->WriteHTML('Your Introduction');
$mpdf->AddPage('L','','','','',50,50,50,50,10,10);
$mpdf->WriteHTML('Your Book text');
$mpdf->AddPage('P','','','','',50,50,50,50,10,10);

Categories