ScreenshotNeo

BlogHow-to

How to Make Html2Pdf.app Wait for JavaScript Before Capturing a Page

Set Html2Pdf.app’s waitFor value in seconds to give JavaScript and asynchronous assets time to render before PDF generation.

By the ScreenshotNeo team4 October 20266 min read

To give JavaScript-rendered content time to appear before Html2Pdf.app generates a PDF, set waitFor in the JSON request body. The value is an integer number of seconds from 0 to 10; the default is 0. For example, "waitFor": 3 adds a three-second wait before generation.

Send the request to POST https://api.html2pdf.app/v1/generate, with your API key in the X-API-Key header. The official Html2Pdf.app documentation describes waitFor as a fixed delay for JavaScript and asynchronous resources. It does not document a selector wait or readiness callback, so the delay does not prove that every script has finished.

1. Set the wait in the JSON request

Include waitFor alongside html. That field accepts a publicly reachable page URL or raw HTML markup.

{
  "html": "https://example.com/report",
  "waitFor": 3
}

Use an integer from 0 through 10. The setting applies to that generation request; it is not a persistent account-wide setting. Start with a short delay and increase it only if the output shows that the page needs more time. That is practical tuning guidance, not a vendor-tested recommendation.

2. Send a complete request

cURL

curl --request POST \
  --url https://api.html2pdf.app/v1/generate \
  --header 'X-API-Key: YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"html":"https://example.com/report","waitFor":3}' \
  --output report.pdf

The command writes the response body to report.pdf. Keep the API key private; avoid committing it to source control or printing it in shared logs.

Python

import os
import requests

api_key = os.environ["HTML2PDF_API_KEY"]
response = requests.post(
    "https://api.html2pdf.app/v1/generate",
    headers={
        "X-API-Key": api_key,
        "Content-Type": "application/json",
    },
    json={"html": "https://example.com/report", "waitFor": 3},
    timeout=90,
)
response.raise_for_status()

with open("report.pdf", "wb") as pdf_file:
    pdf_file.write(response.content)

Install the HTTP client with python -m pip install requests if it is not already present. The timeout shown is a client-side example, not an Html2Pdf.app service limit.

Node.js

const apiKey = process.env.HTML2PDF_API_KEY;
if (!apiKey) throw new Error("Set HTML2PDF_API_KEY first");

const response = await fetch("https://api.html2pdf.app/v1/generate", {
  method: "POST",
  headers: {
    "X-API-Key": apiKey,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    html: "https://example.com/report",
    waitFor: 3,
  }),
});

if (!response.ok) {
  throw new Error(`PDF request failed: ${response.status} ${await response.text()}`);
}

const pdf = Buffer.from(await response.arrayBuffer());
await import("node:fs/promises").then(({ writeFile }) =>
  writeFile("report.pdf", pdf)
);

This uses the built-in fetch available in current Node.js releases. If using a runtime without global fetch, use its installed HTTP client and preserve the same URL, headers, and JSON body.

3. Choose a delay and check the result

  1. Generate the PDF with a small waitFor value.
  2. Inspect the page where JavaScript content is missing or incomplete.
  3. Increase the delay by a small amount, staying at or below 10 seconds.
  4. Check that the target page and its required resources are publicly reachable.
  5. Confirm the CSS media mode matches the layout you want in the PDF.

A fixed delay adds at least that much waiting time to the capture process. It can help with content that appears after a predictable delay, but it cannot guarantee readiness when scripts take an unpredictable amount of time, fail, or keep running. The documentation reviewed describes only a time-based waitFor control.

4. Check fonts, resources, and media mode

Waiting helps only if the page can load what it needs. Html2Pdf.app’s documentation says the page URL and fonts or stylesheets must be publicly reachable. It also identifies media mode and JavaScript loading time as factors that can affect the result.

  • Public access: A page behind a login, private network, or firewall may not be available to the converter. Use a publicly reachable URL or provide raw HTML where appropriate.
  • External assets: Check that font files, stylesheets, and other required resources can be fetched publicly. The documentation specifically notes that asynchronously loaded fonts may need waitFor.
  • CSS media: The documented media option accepts screen or print, and defaults to screen. Choose the mode that matches the intended output; changing the delay will not correct a layout difference caused by media styles.
  • Raw markup: If sending HTML directly, ensure its linked assets are accessible to the renderer. Raw markup does not make private external resources public.

See the official parameter and rendering documentation for the current request schema. The research source establishes the media values and default, but not a complete list of all PDF options, so this guide does not infer additional parameter names.

5. Troubleshooting missing JavaScript content

Symptom Likely cause What to check
JavaScript-generated section is absent The content appears after the configured delay, or its script did not run. Increase waitFor gradually, up to 10 seconds. Check whether the page itself renders the section and whether its scripts load successfully.
Some text appears, but charts or images are missing Asynchronous resources are still loading or are inaccessible. Check public access to those assets and allow more time within the documented maximum.
Web fonts are replaced or text wraps differently Font files or stylesheets cannot be fetched, or fonts load asynchronously. Make the font resources publicly reachable and try a suitable waitFor value.
The PDF layout differs from the browser The selected media mode applies different CSS rules. Check whether screen or print is appropriate for the desired rendering.
The request is rejected for an invalid wait value waitFor is outside the documented integer range. Send an integer from 0 to 10; the default is zero if omitted.
The PDF is still incomplete at 10 seconds A fixed delay may not match the page’s loading behavior, or a dependency may fail. Check script and asset availability and whether the page can render reliably from a public request. The documented wait option cannot signal a custom page-ready condition.

6. Performance, reliability, and cost considerations

Use the shortest delay that produces the required content. Longer waits increase the time each request spends waiting, and concurrent captures can therefore take longer to finish. A delay also does not repair failed scripts, blocked assets, or inaccessible URLs.

The official documentation reviewed gives the waitFor range and default, but does not establish a universal correct delay, a completion guarantee, or a performance benchmark. Choose the value using the behavior of your own page and inspect representative PDFs, especially after changing page scripts or resources. No pricing or billing claims are made here because they are not established by the cited documentation.

7. Or skip the browser setup

If you need screenshots or PDFs without managing browser capture infrastructure, ScreenshotNeo provides a website screenshot API and MCP server. A single GET request can return a screenshot or PDF. For a screenshot, the request looks like this; see the ScreenshotNeo API documentation for parameters and PDF options.

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

ScreenshotNeo accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers identify the page verdict and billing status. Its MCP server gives AI agents tools for screenshots, page information, and PDF capture. The free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots.

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

FAQ

Is waitFor measured in milliseconds?

No. The documented unit is seconds, and the value is an integer from 0 to 10.

What happens if I omit waitFor?

The documented default is zero seconds.

Does a three-second wait guarantee that all JavaScript has finished?

No. It is a fixed delay intended to give rendering and asynchronous loading more time. Html2Pdf.app’s reviewed documentation does not describe a selector-based wait or a page readiness callback.

Can I use waitFor with raw HTML?

Yes. The html field accepts raw HTML markup as well as a publicly reachable page URL. Linked resources still need to be reachable by the renderer.