Prem Shankar
Prem Shankar

Reputation: 31

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

Upvotes: 0

Views: 1276

Answers (1)

don
don

Reputation: 4522

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

Upvotes: 2

Related Questions