Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
HTML to PNG

How to Convert HTML Code to a PNG Image

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert HTML to a PNG, render the markup in a browser engine and capture the rendered page. Browser automation libraries such as Puppeteer and Playwright can load a URL, HTML string, or local file and save the result as a PNG. This captures what users see—including CSS, images, fonts, and JavaScript output—rather than producing an image of the HTML source code.

Choose what you are converting

Your implementation depends on the input:

  • HTML string: inject it into a new browser page with setContent (Puppeteer) or page.setContent (Playwright).
  • Local HTML file: open it with a file:// URL, or read its contents and set the page content.
  • Public URL: navigate to the address and wait for the page to render.

Before coding, decide whether the output should show the current viewport, one element, or the entire scrollable document. Also choose a viewport size, device scale, background behavior, and a wait condition for dynamic content.

Convert an HTML string with Puppeteer

Puppeteer launches Chromium, creates a page, renders your markup, and writes a PNG. Install it in a Node.js project first:

npm install puppeteer

This complete example creates a self-contained document. The inline CSS makes the result independent of external stylesheets.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
import puppeteer from 'puppeteer';

const html = `


  
  

Rendered HTML

This becomes a PNG.

`; const browser = await puppeteer.launch(); try { const page = await browser.newPage(); await page.setViewport({ width: 1200, height: 800, deviceScaleFactor: 1 }); await page.setContent(html, { waitUntil: 'networkidle0' }); await page.screenshot({ path: 'output.png', type: 'png' }); } finally { await browser.close(); }

page.screenshot returns image data and accepts a file path. PNG is one of Puppeteer’s supported formats. The networkidle0 wait is useful when your markup loads remote assets, but use a more specific condition when a page keeps polling or streaming.

Capture a public URL

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();
  await page.setViewport({ width: 1440, height: 900 });
  await page.goto('https://example.com', { waitUntil: 'networkidle2', timeout: 90000 });
  await page.screenshot({ path: 'example.png', fullPage: true, type: 'png' });
} finally {
  await browser.close();
}

fullPage: true expands the capture to the document’s scrollable height. Without it, only the visible viewport is saved.

Capture one element

const card = await page.locator('.card');
await card.screenshot({ path: 'card.png', type: 'png' });

Element screenshots are useful for invoices, charts, cards, and components. Ensure the selector identifies exactly the region you want after JavaScript has finished rendering.

Transparent PNGs

await page.screenshot({ path: 'transparent.png', type: 'png', omitBackground: true });

Puppeteer’s omitBackground removes the browser’s default background. It cannot remove a background color explicitly painted by your HTML or CSS, so set the page or target element’s background to transparent as well.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Playwright instead

Playwright offers Chromium, Firefox, and WebKit automation and documents viewport, element, full-page, and buffer screenshots through its Page API. Install the package and browser binaries:

npm install playwright
npx playwright install chromium
import { chromium } from 'playwright';

const html = '

Hello PNG

Playwright rendered this.

'; const browser = await chromium.launch(); try { const page = await browser.newPage({ viewport: { width: 1000, height: 700 } }); await page.setContent(html, { waitUntil: 'networkidle' }); await page.screenshot({ path: 'playwright.png', type: 'png', fullPage: true }); } finally { await browser.close(); }

Playwright documents PNG as the default screenshot type, so type: 'png' is explicit rather than required. To process the image in memory, omit path and receive a buffer:

const image = await page.screenshot({ type: 'png', fullPage: true });
// image is a Buffer; send it to storage or an HTTP response.

Local files, fonts, images, and JavaScript

Open a local file

import path from 'node:path';
import { pathToFileURL } from 'node:url';

await page.goto(pathToFileURL(path.resolve('invoice.html')).href, {
  waitUntil: 'networkidle0'
});
await page.screenshot({ path: 'invoice.png', type: 'png', fullPage: true });

Relative CSS, image, and font paths must resolve from the file’s directory. A local file that fetches remote resources may also encounter browser security rules; serving the folder through a local HTTP server is often more predictable.

Wait for dynamic content

Network-idle waits are not universal. A page may finish its network activity before a chart appears, or never become idle because of analytics or live updates. Prefer a known selector, a short deliberate delay, or an application-specific readiness flag:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.goto(url, { waitUntil: 'domcontentloaded' });
await page.waitForSelector('#report-ready', { timeout: 30000 });
await page.screenshot({ path: 'report.png', fullPage: true });

For web fonts, wait until document.fonts.ready; for lazy images, scroll or trigger the application’s loading behavior before capture.

Capture choices that change the PNG

Decision What it does Typical setting
Viewport Captures only the visible browser area. Omit fullPage.
Full page Captures the complete scrollable document. fullPage: true.
Element Captures one matched component. locator.screenshot() or an element handle.
Resolution Controls CSS dimensions and pixel density. Set viewport and deviceScaleFactor.
Background Controls whether the default canvas is painted. Puppeteer omitBackground: true.
Format Chooses the encoded image. type: 'png'.

Large full-page captures consume more memory and may expose browser limits on extremely tall documents. Split very long reports into sections when a single image is impractical.

Higher-level PHP option: Browsershot

Spatie Browsershot provides a PHP interface for converting a URL or supplied HTML to an image, PDF, or string while using Puppeteer and headless Chrome underneath. It can reduce JavaScript in a PHP application, but verify its current Node.js, Puppeteer, Chrome, and PHP compatibility before installation. The underlying browser-rendering decisions—viewport, waits, external assets, and full-page capture—still apply.

Troubleshooting HTML-to-PNG conversion

The PNG is blank

  • Wait for a selector that proves the application rendered, rather than capturing immediately after navigation.
  • Check that the target has dimensions and is not hidden by CSS.
  • Inspect console errors and failed network requests; a JavaScript exception can prevent the page from building.

Images or fonts are missing

  • Use absolute, reachable URLs or correct paths relative to a local file.
  • Wait for document.fonts.ready and for image elements to report completion.
  • Make sure the capture environment can access private assets, authentication endpoints, and certificates.

The image is cropped

  • Use fullPage: true for the whole document.
  • For a component, capture the element after layout settles and check overflow rules.
  • Increase viewport width if responsive CSS moves content at a narrower breakpoint.

Transparent output still has a color

omitBackground only affects the browser’s default canvas. Remove explicit background declarations from the page or target element if transparency is required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Navigation times out

Confirm the URL is reachable from the machine running the browser, raise the timeout only when the page is legitimately slow, and avoid waiting for network idle on sites with permanent connections. A selector-based readiness check is usually more reliable.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server. One GET request renders a URL and returns PNG, JPEG, WebP, or PDF. It removes cookie and consent banners, newsletter popups, and chat widgets before capture; bot checks, CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing result.

Use the API documentation at https://screenshotneo.com/docs/ for all parameters. The simplest PNG 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

Change the output filename and format options as needed. The API supports full-page capture with lazy images loaded, CSS-selector element capture, dark mode, 12 device presets or any viewport, retina scale, PDF paper and page controls, custom CSS and JavaScript, pre-capture clicks, hidden selectors, waits for a selector, delay, or network idle, request and resource blocking, custom headers, cookies, user agents and Authorization, timezone and geolocation, transparent backgrounds, resizing, configurable-TTL caching, signed links for public image tags, asynchronous jobs with signed webhooks, bulk capture of up to 100 URLs per call, a usage API, and an OpenAPI specification. Parameter names used by other screenshot APIs are accepted to ease migration.

Python

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)

Node.js

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const fs = await import('node:fs/promises');
await fs.writeFile('shot.webp', Buffer.from(await res.arrayBuffer()));

An MCP server provides take_screenshot, get_page_info, and capture_pdf tools to Claude, Cursor, or another MCP client, so an AI agent can perform captures without custom browser code. Plans include 1,000 shots per month free with no card; paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Cost, reliability, and operating practices

  • Self-hosted browsers: no per-shot API fee, but you maintain browser binaries, workers, memory limits, fonts, network access, and concurrency.
  • Hosted capture: avoids browser installation and can centralize retries, authentication, caching, and bulk jobs; review each provider’s billing and failure rules.
  • Deterministic images: pin viewport, device scale, timezone, locale, fonts, and wait conditions. Disable animations or inject CSS when visual diffs matter.
  • Security: treat URLs, cookies, headers, and HTML as sensitive inputs. Restrict server-side navigation to prevent access to internal network addresses, and avoid logging credentials.
  • Performance: reuse browser processes for batches, limit concurrency, block unnecessary resources, and cache stable pages. Full-page images and high device scale increase memory and file size.

FAQ

Can I convert HTML to PNG without opening a visible browser window?

Yes. Puppeteer, Playwright, Browsershot, and ScreenshotNeo use headless browser rendering; no interactive window is required.

Does converting HTML preserve CSS?

Rendered CSS is reflected in the image when stylesheets and assets load successfully. Unsupported browser features, missing fonts, or timing problems can change the result.

Can I return the PNG directly from an API endpoint?

Yes. Capture to a memory buffer with Playwright or Puppeteer, set the response content type to image/png, and send the buffer without writing a file.

Why is my page different on a server than on my laptop?

Browser version, installed fonts, viewport, device scale, timezone, locale, network access, authentication, and animation timing can all affect rendered pixels. Pin these variables when reproducibility matters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Can I convert HTML to PNG in a browser-only frontend?

Client-side libraries can rasterize limited markup, but a real browser automation process is more reliable for external CSS, web fonts, JavaScript, and full-page capture.

What image format should I use if I need transparency?

Use PNG and remove explicit page backgrounds; Puppeteer’s omitBackground option can then preserve transparency in the default canvas.

The Bottom Line

Render the HTML in Chromium or another browser engine, wait for the content and assets you need, then save a PNG from the viewport, an element, or the full page. Puppeteer and Playwright provide direct control; Browsershot suits PHP applications; ScreenshotNeo removes browser setup when you prefer an API or MCP workflow.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.