mPDF pagination problem when using WriteHTML function - php

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

Related

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

TCPDF content grouping - prevent section from being on different page

I am using TCPDF and using the writeHTML function to create PDF's like this:
$html = 'html content';
$pdf->writeHTML($html, true, false, true, false, '');
I have sections of content and each section is in a div and table, as the page and content automatically continues over to the next page, sometimes i get a very few lines of my content on one page and the remaining on the next page.
Is there a way in TCPDF, to tell it to make sure that the entire content of a div or table should be together? so if it needs to go to the next page, it should take the entire div to the next page?
Thanks
* UPDATE *
As olger suggested, 'page-break-inside' style attribute seems to work and do the job below:
<div style="page-break-inside:avoid;">
HTML grouped content
</div>
If you don't want breakable text then add this style div or table which you used. Add this style in your code style="page-break-inside:avoid;"

How to get the total number of pages in MPDF?

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...

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

Is there any way to put a footnote in MPDF document?

I would like to put a footnote in my PDF, created by MPDF from html. I didn't find any function or tag for that. I tried put notes in footer, changing it by tag, but that was changing footers for all even/odd pages. Maybe is there a way to change footer starting from next page? Then I would change footer with my note text and immediately after change it to standard footer.
$mpdf->SetFooter('{DATE d/m/Y }|{PAGENO}/{nb}|Sym Consultoria');

Categories