How to handle PDF pagination in PhantomJS

Rayjax picture Rayjax · Jun 11, 2013 · Viewed 25.7k times · Source

I am using PhantomJS to create PDFs from html.

It works fine, but I can't find out how to work with pagination; I want to create a page for each div in my document, but I can't find anything in the doc. about pagination.

If my document is short, it makes only one page, and if it is bigger, it creates one second empty page and my contents are in the first page which becomes very long.

Any idea ? (I am using phantomJS-node module for nodeJS)

Answer

Cybermaxs picture Cybermaxs · Jun 11, 2013

PhantomJS takes care of webkit’s css implementation. To implement manual page breaks you can use these properties :

  • page-break-before : auto/always/avoid/...
  • page-break-inside : auto/always/avoid/...
  • page-break-after : auto/always/avoid/...

For example, a div can be :

 <div style="page-break-before:always;"><!-- content --></div>

or

<div style="page-break-after:always;"> <!-- content --></div>

Controlling page breaks when printing in Webkit is sometimes not easy, in particular with long html tables.