Print page breaks with wkhtmltopdf or Headless Chrome

August 21st, 2018 / by api2pdf /

Intro

It is quite common, when tasked with generating a PDF report, or PDF book of some kind that you are required to break up the PDF into separate pages, or items. Typically an item will span multiple pages, and you want the next item in the PDF to start on a brand new page, regardless of where the previous item ended. This is called a page break, and there is a simple trick to force a page break when using wkhtmltopdf and Headless Chrome.

Api2Pdf is an easy-to-use and scalable web service that generates PDFs from HTML, URLs, and Microsoft Office files. The page break trick can be used with Api2Pdf the same way you would use it with wkhtmltopdf or Headless Chrome. Api2Pdf is a wrapper for these PDF engines.

Page Break CSS for PDF Generation

In order to force a page break or new page in a PDF for wkhtmltopdf or Headless Chrome, you need to use the page-break rules. The easiest way is to setup a <div> tag and then apply the style directly to the <div> that wraps the content you want to appear on individual pages. The recommended style choice is “page-break-before:always;” See the sample HTML below.

That’s all there is to it. You can easily test this out by going to the home page of Api2Pdf.com and clicking Try It Out. Paste the HTML in there and generate the PDF.

Tags: , , , ,

Comments are closed.