ScreenshotNeo

BlogHow-to

How to Set Page Size and Margins in Html2Pdf.app

Set standard or custom PDF page dimensions and control each margin in Html2Pdf.app. Includes request examples, rendering guidance, and troubleshooting.

By the ScreenshotNeo team4 October 20264 min read

In Html2Pdf.app, set a standard page size with format, choose orientation with landscape, and set whitespace independently with marginTop, marginRight, marginBottom, and marginLeft. For a custom page size, provide both width and height as integer pixel values. The documented endpoint is an authenticated JSON POST to https://api.html2pdf.app/v1/generate.

1. Choose a standard or custom page size

Use format when the output should use a named paper format. The documented choices are Letter, Legal, Tabloid, Ledger, and A0 through A6. The documented default is A4. Set landscape to true for landscape orientation; it defaults to false.

If a named format does not fit, supply both width and height as integer pixel dimensions. The documentation describes these fields together as the custom-size option. Do not rely on specifying only one dimension as a complete custom page size.

Need Fields Notes
Named paper size format A4 is the documented default; supported names include Letter, Legal, Tabloid, Ledger, and A0–A6.
Landscape page landscape: true Orientation is separate from the selected format. Default is false.
Custom dimensions width and height Supply both, as integer pixel values.

2. Set each margin independently

Use marginTop, marginRight, marginBottom, and marginLeft to control the whitespace between the page edges and rendered content. Each value is in pixels, and each documented default is 0. You can use different values on each side; there is no need to make all four equal.

For example, the following payload uses 24 pixels on every side as an illustrative choice, not as a documented recommendation or default:

{
  "html": "https://example.com/report",
  "format": "A4",
  "landscape": false,
  "marginTop": 24,
  "marginRight": 24,
  "marginBottom": 24,
  "marginLeft": 24
}

3. Send an authenticated generation request

The API expects an X-API-Key header and JSON request body. The html field can contain a publicly reachable URL or raw HTML. This cURL example uses a URL; replace the API key and target as needed.

curl -X POST "https://api.html2pdf.app/v1/generate" \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "https://example.com/report",
    "format": "A4",
    "landscape": false,
    "marginTop": 24,
    "marginRight": 24,
    "marginBottom": 24,
    "marginLeft": 24
  }' \
  --output report.pdf

To use a custom size, send both dimensions as integers, for example "width": 800 and "height": 1100. These are example pixel values. Consult the official Html2Pdf.app documentation and cURL examples for the current request reference.

4. Verify layout with representative documents

Html2Pdf.app documents that conversion runs in headless Chromium and supports modern HTML, CSS, and JavaScript. Output can still vary with CSS media mode, available fonts and other resources, and JavaScript load timing. Test representative documents before production use, especially when changing page dimensions, orientation, or margins.

  1. Start with the intended format or a complete custom width and height pair.
  2. Set orientation explicitly if the layout depends on landscape pages.
  3. Set all four margins explicitly when the output needs predictable whitespace; otherwise the documented margin defaults are zero.
  4. Generate PDFs from representative input, including pages with long content, loaded fonts, images, and client-rendered sections used by your application.
  5. Inspect page breaks, clipped content, and spacing, then adjust dimensions or margins and repeat.

The request fields specify the PDF page geometry and edge whitespace. They do not guarantee that the source document’s CSS, assets, or JavaScript will produce the same result in every rendering environment.

5. Troubleshooting page size and margins

Symptom Likely cause What to check
Output is A4 when another size was expected format was omitted or not set as intended. Set a supported format explicitly, or provide both custom dimensions.
Custom page dimensions do not take effect Only one of width or height was supplied, or the values are not integer pixels. Send both fields as integer pixel values.
Content sits against the page edge The margin fields default to zero. Set the relevant side fields; specify all four if consistent, explicit geometry is needed.
Whitespace is uneven One or more side-specific margin values differ or were omitted. Check top, right, bottom, and left values independently.
Page is portrait instead of landscape landscape is omitted or false. Set landscape to true.
Text, images, or dynamic sections are missing or shifted Rendering conditions such as fonts, external resources, CSS media mode, or JavaScript timing affect output. Check resource availability and render timing, then test with representative source documents as the documentation recommends.

6. Performance, reliability, and cost considerations

Page size and margins are geometry choices, not performance guarantees. Larger or more complex source documents may involve more rendering work; the supplied Html2Pdf.app documentation does not establish a benchmark or a cost figure for particular page dimensions. Avoid inferring a conversion time or price from the selected format alone.

For reliable output, keep source URLs and required resources reachable by the renderer, ensure fonts and images are available, and validate pages that depend on JavaScript. Retest when the HTML, CSS, assets, rendering settings, or geometry changes. The documentation specifically recommends testing representative documents before production use.

7. Or skip the browser setup

If your task is capturing a web page as an image rather than generating a paginated PDF with Html2Pdf.app, ScreenshotNeo provides a website screenshot API and MCP server. Its one-call endpoint returns PNG, JPEG, WebP, or PDF output; for a screenshot, the basic request is:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Cookie banners, newsletter popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots per month with no card, and paid plans start at $5 for 3,000.

Sign up free for 1,000 screenshots a month, with no card required.

FAQ

What happens if I leave out all margin fields?

Each margin defaults to zero according to the documentation.

Can I set only the width for a custom page?

The documented custom-size option uses both width and height, as integer pixel values.

Does landscape replace the page format?

No. format selects a named page size, while landscape controls orientation.