Back to blog
html to png apiscreenshot apiog image generationweb developmentpdf generation

Turning HTML into PNG: The API Approach That Actually Scales

Looking for an html to png API that handles real-world HTML without headaches? Here's how to convert HTML to PNG reliably, with code examples and gotchas.

2026-12-015 min read

Converting HTML to a PNG sounds trivial until you try it yourself. You spin up a headless browser, write a render function, and everything works great on your laptop. Then it hits a server with no GPU, no fonts installed, and a cookie banner that eats half the viewport. Suddenly your "simple" screenshot generator is a maintenance project.

This post walks through the actual problems you'll hit converting HTML to PNG programmatically, and how an API-based approach sidesteps most of them.

Why HTML to PNG Is Harder Than It Looks

Rendering HTML to an image isn't just "take a screenshot." The HTML you're converting usually falls into one of two categories:

  • A live URL, like a product page, dashboard, or article you want to capture as-is
  • Raw HTML/CSS you generate, like an OG image template or invoice layout, that needs to be rendered before it's turned into a PNG

Both cases require an actual browser engine to render correctly, because CSS Grid, custom fonts, web fonts loaded via @font-face, and JavaScript-rendered content all need real layout and paint logic. Libraries that fake this (like converting HTML to canvas client-side) break on anything beyond basic layouts: box shadows render wrong, gradients clip, and web fonts often don't load in time.

The DIY Route and Where It Breaks

If you've tried doing this yourself with Puppeteer or Playwright, you've probably hit some combination of:

  1. Missing system fonts on your server (Linux containers don't ship with Helvetica or Arial)
  2. Chrome binary size bloating your Docker image by 300+ MB
  3. Memory leaks from browser instances that don't get cleaned up properly
  4. Timeout issues when a page has slow-loading ads, trackers, or cookie consent scripts
  5. Cold start latency on serverless functions because spinning up headless Chrome takes 2-4 seconds

None of these are unsolvable, but they add up. You end up managing browser pool sizing, retry logic, font packages, and server scaling just to get a PNG out the other end.

The API Approach: One GET Request, One PNG

The alternative is treating HTML-to-PNG conversion as infrastructure you don't own. You send a URL (or an HTML string), and a managed service running real Chrome handles rendering, waits for the page to settle, and returns the image.

With pxshot, this looks like:

GET https://api.pxshot.dev/v1/screenshot?url=https://example.com&format=png

That single request returns a PNG rendered in actual Chrome, at whatever viewport size and device scale factor you specify. No browser management, no font installs, no Docker image bloat.

What You Typically Need to Control

A good html to png API should expose at minimum:

  • Viewport dimensions (width/height) for consistent framing
  • Device scale factor for retina-quality output
  • Full page vs. viewport capture for long pages
  • Wait conditions (network idle, specific selector, fixed delay) so JS-heavy pages finish rendering
  • Output format (PNG, JPEG, WebP, or PDF) depending on use case

Here's a slightly more complete example with some of these parameters:

GET https://api.pxshot.dev/v1/screenshot
  ?url=https://news.ycombinator.com
  &format=png
  &width=1280
  &height=800
  &full_page=true
  &device_scale_factor=2

Common Use Cases and How to Handle Each

1. Generating OG Images Dynamically

If you're building OG images for blog posts or product pages, the usual pattern is:

  1. Build an HTML template with the title, author, and background injected as query params
  2. Host that template at a route like /og?title=My+Post
  3. Pass that URL to the screenshot API to get back a PNG
  4. Cache the result (since OG images rarely change after publishing)

Example:

GET https://api.pxshot.dev/v1/screenshot
  ?url=https://yoursite.com/og?title=My+Post
  &format=png
  &width=1200
  &height=630

1200x630 is the standard OG image size that Twitter, Facebook, and LinkedIn all respect without cropping.

2. Visual Regression and Monitoring

If you're checking that a page hasn't visually broken after a deploy, take a full-page PNG on a schedule and diff it against the previous capture. This is simpler than it sounds:

  • Capture a baseline screenshot and store it
  • On each deploy or cron run, capture a new screenshot of the same URL
  • Diff pixel data (tools like pixelmatch work well for this)
  • Alert if the diff percentage exceeds a threshold

Because the rendering is handled by the API, you avoid flaky results caused by font-loading differences between your local Chrome and your CI environment's headless browser.

3. Converting HTML to PDF for Reports and Invoices

Same underlying problem, different output format. If you're generating invoices, contracts, or reports from HTML templates, swap the format parameter:

GET https://api.pxshot.dev/v1/screenshot
  ?url=https://yoursite.com/invoice/4471
  &format=pdf

This avoids maintaining a separate PDF library (like wkhtmltopdf, which has its own rendering quirks and hasn't seen active development in years).

Handling the Cookie Banner Problem

If you're capturing third-party URLs rather than your own HTML, cookie consent banners are a recurring annoyance. They cover the top third of the page, they're inconsistent across sites, and writing custom logic to dismiss every possible consent framework isn't realistic.

This is one area where it's worth checking what a screenshot API handles by default. pxshot strips cookie banners automatically before capturing, so screenshots of third-party sites come back clean without extra configuration on your end.

Checklist Before You Pick an HTML-to-PNG Solution

Whether you build it yourself or use an API, confirm it handles:

  • Web fonts and custom typography rendering correctly
  • JavaScript-heavy pages (SPAs, client-rendered dashboards)
  • Configurable wait conditions, not just a fixed delay
  • Retina/high-DPI output via device scale factor
  • Multiple output formats without separate integrations
  • Reasonable response times under real traffic, not just in a demo

If you're building something that touches HTML to PNG even occasionally, spending engineering time maintaining a Puppeteer fleet rarely pays off compared to a single API call.

You can try the no-signup demo directly on the pxshot.dev homepage to see raw output before integrating anything, or sign up for the free plan, which includes 50 screenshots a month, to start generating PNGs, JPEGs, WebPs, or PDFs from any URL.