Using an inline CSS style to create a new page in a PDF generator

I’ve recently been working with the TCPDF PDF generator class in PHP to generate PDFs on the fly as part of one of our newer business processes.

The class takes a lump of formatted HTML and creates a PDF document however generation being on the fly, there is no opportunity to use the inbuilt addpage() method of the class when the document needs to be broken up into multiple pages. A good example would be when generating an invoice and attaching a second page containing terms or a new chapter in a book.

Ideally, some marker within the HTML would inform the PDF generator when there is a page break, and thankfully there is.

Enter CSS

Set style="page-break-after:always;"

in the html e.g. for an empty div or p element, wherever you require a page break and TCPDF, and I warrant any good PDF generator function or class, will do the needful.

It’s handy to keep in mind that the same CSS style will also cause a printer to begin printing a new page when encountered.

Many thanks to Peter Young for this useful nugget.