Tutorials

How to Convert a Chrome HTML Document to PNG: Four Ways That Work

How to Convert a Chrome HTML Document to PNG: Four Ways That Work

You double-click a saved web page, Windows tells you its type is "Chrome HTML Document", and what you actually need is a PNG: something you can drop into a slide, attach to an email, or upload to a system that only accepts images. Renaming the file to .png does nothing useful, and Chrome has no "Save as image" in its menus.

There are four dependable ways to do it, and they suit different jobs. One is a command tucked away inside Chrome. One drives Chrome from the command line. One is a free online converter. One is an API call for when there are hundreds of files. The command-line and API output below is real, produced with Chrome 158 from the same test file, and each section ends with the thing that route will not warn you about.

What a "Chrome HTML Document" actually is

It is an ordinary .html file. "Chrome HTML Document" is not a format. It is the label File Explorer shows in its Type column for .html and .htm files when Chrome is your default browser. Make Edge the default and the same file becomes a "Microsoft Edge HTML Document". With Firefox it is a "Firefox HTML Document". The contents never change.

That matters because of what the file holds. HTML is a set of instructions: put a heading here, load that font, draw a table with these rows. A PNG is a grid of pixels. Getting from one to the other means something has to follow the instructions and draw the page, which is the job of a browser engine. Every method below is the same idea underneath: let Chrome render the page, then save what it drew.

The test file

Each route gets the same file, report.html: a quarterly report with a heading, three summary tiles in a CSS grid, a 13-row table and a web font loaded from Google Fonts. Two details make it behave like a real page. At 1,280 pixels wide it is 1,077 pixels tall, so it does not fit on one screen. And the revenue figure is filled in by a script 600 milliseconds after the page loads, the way a number fetched from a server would be:

<div class="tile">Revenue <b id="rev">...</b></div>

<script>
  setTimeout(() => {
    document.getElementById('rev').textContent = '£228,010';
  }, 600);
</script>

Running this in production? Get an API key with 50 free credits, no card needed.

A converter that fires too early captures the three dots instead of the number.

Method 1: Chrome's built-in full-page screenshot

Chrome can already save a page as a PNG. The command lives in DevTools, not in a menu.

  1. Open the HTML file in Chrome. Double-clicking is enough if Chrome is your default browser. Otherwise drag the file onto a Chrome window.

  2. Open DevTools with F12, or Ctrl+Shift+I on Windows and Linux, or Cmd+Option+I on a Mac.

  3. Open the command menu with Ctrl+Shift+P, or Cmd+Shift+P on a Mac.

  4. Type screenshot and choose Capture full size screenshot.

Chrome saves a PNG of the whole page, including everything below the fold, to your Downloads folder. The same menu offers Capture screenshot for the visible area only and Capture node screenshot for whichever element is selected in the Elements panel, which is the quick way to grab one chart or one table.

The image is as wide as the page area of your Chrome window at that moment. To choose the width, switch on the device toolbar first with Ctrl+Shift+M, type a width into the dimensions box at the top of the page, then run the command.

What it will not tell you: none of this is repeatable. The width depends on how large your window happened to be, and DevTools docked at the side makes the page narrower than it looks. The tenth file is exactly as much clicking as the first. This is the right tool for one file, today.

Method 2: headless Chrome on the command line

Chrome has a headless mode that loads a page without opening a window, and a --screenshot flag that writes what it loaded to a file. There is nothing extra to install.

First find the Chrome binary. These are the usual locations:

# Windows
"C:\Program Files\Google\Chrome\Application\chrome.exe"

# macOS
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

# Linux
google-chrome

The examples below use chrome to stand for whichever of those applies. The flags are the same on all three, and since Chrome 132 a plain --headless runs the current headless mode, so older advice about --headless=new no longer applies. The shortest command that works is:

chrome --headless --screenshot report.html

That writes screenshot.png into the current directory in about a second. It is also wrong in four ways.

Two outputs from headless Chrome for the same report.html. On the left, the bare --screenshot command: a 780 by 493 image, cut off after the second table row, with a scrollbar on the right edge and three dots where the revenue figure should be. On the right, the same file with --window-size, --hide-scrollbars and --virtual-time-budget: a 1280 by 1200 image showing all thirteen table rows and the revenue figure of £228,010

The default image was 780×493 pixels. The page was cut off after the second table row, a scrollbar ran down the right-hand edge, and the revenue tile showed three dots because the capture fired before the script ran. Chrome still reported success: 38125 bytes written to file screenshot.png.

Each problem has a flag.

Set the size. --window-size=1280,720 produced an image of exactly 1280×720.

Remove the scrollbar. --hide-scrollbars does what it says.

Capture the whole page. There is no full-page flag. The screenshot is the window, so the window has to be at least as tall as the page. The test page is 1,077 pixels tall, so --window-size=1280,1200 captured all of it, plus 123 pixels of empty background at the bottom to crop off afterwards.

Wait for late content. --virtual-time-budget=2000 lets up to two seconds of timers and loading play out before the capture. With it, the revenue figure appeared.

Put together, with an explicit output name:

chrome --headless \
  --screenshot=report.png \
  --window-size=1280,1200 \
  --hide-scrollbars \
  --virtual-time-budget=2000 \
  report.html

On Windows, write it on one line. The trailing backslashes are a Unix shell convention.

That produced a correct 1280×1200 PNG in 1.06 seconds. A few more flags are worth knowing, each checked against the same file:

Flag

What it is for

Result

--force-device-scale-factor=2

Double-density output, for sharp text on high-resolution screens

2560×2400 from the same window size

--default-background-color=00000000

A transparent background wherever your CSS does not set one

A PNG with an alpha channel and fully transparent corners

--screenshot=report.jpg

Choosing the format by file extension

A JPEG. .webp worked too

--print-to-pdf=report.pdf

A PDF instead of an image

A two-page PDF

For a folder of files, loop over them:

for f in *.html; do
  chrome --headless --screenshot="${f%.html}.png" \
    --window-size=1280,1200 --hide-scrollbars \
    --virtual-time-budget=2000 "$f"
done

What it will not tell you: Chrome writes a file and exits cleanly whether or not the page was ready. The cropped, half-loaded default image and the correct one both ended with the same "bytes written" line. The time budget is a guess you make for each page, the window height is a number you have to know in advance, and the machine needs every font the page expects. For a folder of your own files on your own computer, this is quick and free. In a scheduled job on a server those quiet failures are the ones a customer finds first, and running Chrome on AWS Lambda adds a few more of its own.

Method 3: a free online converter

If you would rather not open DevTools or a terminal, the HTML to Image Converter does the rendering on a server.

  1. Open your HTML file in a text editor. On Windows, right-click it, choose Open with, then Notepad. Select everything and copy it.

  2. Paste it into the converter.

  3. Set the width and height you want. They default to 1200 by 630.

  4. Convert, then download the PNG or copy its hosted URL.

The page is drawn by a current Chrome, so grid layouts, web fonts and gradients come out the way they look in your own browser.

What it will not tell you: a server cannot see your hard drive. If the page refers to logo.png or style.css sitting in the same folder as the HTML file, those references point at nothing once the markup is pasted somewhere else, and the image comes back unstyled or with gaps. This catches out pages saved from Chrome as "Webpage, Complete", because that option puts the images and stylesheets in a separate folder next to the file. Self-contained pages, and pages that load their assets from public URLs, convert cleanly. For a page that depends on local files, methods 1 and 2 are the ones that can read them.

If the page is live on the web, skip the file entirely and give its address to the website screenshot tool.

Method 4: an API call, for more than a handful

When the job is every invoice, every nightly report or one image per customer, send the HTML to the HTML to Image API. It is one POST request with the markup in a JSON body, and jq takes care of the escaping:

jq -Rs '{html: ., width: 1280, fullpage: true, ms_delay: 1000}' report.html \
  | curl -s -X POST https://app.html2img.com/api/html \
      -H "X-API-Key: $HTML2IMG_API_KEY" \
      -H "Content-Type: application/json" \
      -d @-

The response is JSON that includes the address of the finished image:

{
  "success": true,
  "id": "6c26477f-d49e-4637-9672-80be350d6a85",
  "url": "https://i.html2img.com/image-1796381915150-147906.png"
}

That request took 2.3 seconds and returned a 1280×1077 PNG. That is the exact height of the page with nothing to crop, because fullpage measures the document for you. The 600 ms script was covered by ms_delay. Where a page can signal that it has finished, wait_for_selector waits for a specific element to exist, which beats guessing a number.

One detail on size. Full-page captures render at a device pixel ratio of 1. Fixed-size captures from the HTML endpoint default to 2, so a 1200×630 request comes back at 2400×1260 unless you pass "dpi": 1.

You do not have to use curl. There are official clients for PHP, Laravel, JavaScript, Python and Ruby on the integrations page, and the walkthrough on generating a screenshot with curl covers the raw request in more depth. If your starting point is a spreadsheet and one template, not a folder of finished files, bulk image generation from a spreadsheet is the closer fit.

What it will not tell you: the same thing as the online converter. The API cannot read files from your disk, so local images and stylesheets need inlining or hosting first. It also spends a credit per image, where your own Chrome costs nothing but your time.

PNG, JPG or PDF?

PNG is the right default for anything with text in it. It is lossless, so letters stay crisp, and it supports transparency.

JPG and WebP make smaller files. For the 1280×720 capture of the test page, Chrome wrote a 56 KB PNG, a 48 KB JPEG and a 21 KB WebP. On a page that is mostly text the saving from JPEG is small and you lose transparency, so it only earns its place on photo-heavy pages or where an upload form insists on it.

PDF is a different kind of output, and often the one you wanted all along. If the destination is a printer, a signature or a document archive, a PDF keeps the text selectable and splits the content across pages. Chrome's --print-to-pdf put the test page on two US Letter pages with a date, title, file path and page number printed in the margins. Adding --no-pdf-header-footer removed those, and @page { size: A4; } in the page's CSS switched the paper to A4. Without a terminal, the HTML to PDF converter does the same job in the browser, the HTML to PDF API handles volume, and the guide to page breaks in HTML to PDF covers keeping tables and headings together.

Which method to use

DevTools

Headless Chrome

Online converter

API

Best for

One file, right now

A folder of files on your own machine

One file, nothing to install

Many files, or a scheduled job

You need

Chrome

Chrome and a terminal

Any browser

An API key

Whole page in one image

Yes, built in

Only if you set the window height

You set the height

Yes, with fullpage

Reads local images and CSS

Yes

Yes

No

No

Repeatable

No

Yes

No

Yes

When the PNG comes out wrong

It is cut off at the bottom. The capture area was shorter than the page. Raise the window height in headless Chrome, use the full size command in DevTools, or set fullpage on the API. The full-page screenshot guide goes into the edge cases, such as sticky headers and lazy-loaded images.

Numbers, charts or whole sections are missing. The capture ran before a script finished. Give it a time budget, a delay, or a selector to wait for.

Images are missing or the page is unstyled. The HTML points at files that the renderer cannot reach. Relative paths only work when the files sit next to the HTML on the machine doing the rendering.

The text looks soft. The image was rendered at one device pixel per CSS pixel and then scaled up by whatever is displaying it. Render at a scale factor of 2.

The font is wrong. The font is installed on your computer but not on the machine that drew the image. Load it as a web font in the page itself and it travels with the file.

You only wanted one part of the page. Use Capture node screenshot in DevTools. For a page that is live on the web, the API can crop to a single element by CSS selector.


Turning HTML into images more often than the odd file? Browse the templates gallery or read the docs to get started.

Related articles

Mike Griffiths

Written by

Mike has spent the last 20 years crafting software solutions for all kinds of amazing businesses. He specializes in building digital products and APIs that make a real difference. As an expert in Laravel and a voting member on the PHP language, Mike helps shape the future of web development.

More articles by Mike Griffiths