Skip to main content

How to prevent images from splitting across pages in HTML-to-PDF conversion

Keep image containers together across PDF page breaks using print CSS, with Profiles examples for Zapier, Make and direct API requests.

If an image is split across two PDF pages during HTML-to-PDF conversion, apply print CSS to the div or p that contains it.

Zapier and Make

In the PDF.co action or module, paste the following JSON into the Profiles field:

{
  "HTMLCodeHeadInject": "<style> @media print { div,p { break-inside: avoid !important; } } </style>"
}

Paste only the JSON object above, without an extra quote after the closing brace.

Direct API requests

Add profiles to your existing request body as a JSON-encoded string. The escaped quotes below are required when writing a JSON request body:

{
  "profiles": "{\"HTMLCodeHeadInject\":\"<style> @media print { div,p { break-inside: avoid !important; } } </style>\"}"
}

This is a request-body fragment. Keep the other parameters required by your conversion endpoint, such as html or url.

How the CSS works

break-inside: avoid asks the renderer to keep each selected block together where possible. This example targets div and p elements, so place the image inside one of those containers.

The rule is inside @media print. Use mediaType: "print" for conversion; print is the default for the HTML endpoint. If you have selected screen or none, the print-only rule will not apply.

Keep the image and its container small enough to fit within the printable page area. A block larger than a page cannot be kept together on a single page. This setting controls page breaks; it does not fix missing images or inaccessible image URLs.

Applies to

  • /pdf/convert/from/html

  • /pdf/convert/from/url

Related documentation

Did this answer your question?