PDF Page Breaks

Customizing page break behavior on PDFShift is quite easy. The simplest and most effective way is to leverage the use of a few CSS properties: break-after, break-before, and break-inside. These links jump to the wonderful MDN Web Docs with great explanations of each property and how they behave, so we'll skip ahead on how to effectively use these on PDFshift.

Setting Things Up

It's likely easier to set these properties within your source document's CSS stylesheet or markup. You'll just need to find the element that you wish to initiate a page break on and assign the break-after, break-before, or break-inside property.

Tip: You may use your browser's development console (F12) to quickly find the element you wish to target for a page break. Additionally, you may initiate a "print preview" to see how your new CSS properties impact document flow.

If you can't, or don't wish to, access your source document, you may use the CSS parameter when sending your POST request to PDFShift. This parameter allows you to specify CSS that targets a source element. Let's take a look at an example using the following Wikipedia article as our conversion source: https://en.wikipedia.org/wiki/Toxiclionella_haliplex

Example

Here's the body of our initial conversion request without any page breaks added (the sandbox parameter is optional but recommended for testing):

{
    "source": "https://en.wikipedia.org/wiki/Toxiclionella_haliplex",
    "sandbox": true,
}

This looks a little funky, so let's add a page break before the "Distribution" header. Since we don't have access to the source directly, we'll use PDFShift's CSS parameter:

{
    "source": "https://en.wikipedia.org/wiki/Toxiclionella_haliplex",
    "sandbox": true,
    "css": "h2:nth-of-type(2) { break-before: page; }"
}

Great! Now, we've customized the flow of the document by specifying exactly where we want a page to break during conversion. You can use this method on any number of elements to help ensure your document is converted exactly to your specification.

Questions?

We'll be happy to help! Please feel free to reach out (support@pdfshift.io) if you have any questions or issues adding page breaks to your converted PDFs. As always, when making a support request, please send us an example of your source document and the corresponding POST request that you're sending to PDFshift. This will help us help you more efficiently.

Was this helpful?