How to insert mpdf TOC inside first page? - php

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

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

Why MPDF library is always shrinking to single page?

I am using MPDF. My content of the page is dynamic. So even if the content enhances it doesn't make a page break. Rather the content shrinks and font size becomes smaller.
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetDisplayMode('default');
$mpdf->WriteHTML($htmldata);
$mpdf->Output($name,"D");
Here $htmldata is my dynamic long html variable. Here's the sample of the pdf
enter link description here
mPDF resizes tables so that their parts fit the page.
See https://mpdf.github.io/troubleshooting/resizing.html
Use <table autosize="0"> to prevent most of this behaviour.

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.

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

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