Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
FREEDOM251

Freedom251 › Blog › HTML

How to Take a Screenshot of a Local HTML File with Puppeteer

By Freedom251 Team · · 3 min read

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 Puppeteer to open the local file through a properly formed file:// URL, then save the page with page.screenshot(). This runnable Node.js example captures the full page as a PNG:

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

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();
  await page.setViewport({ width: 1280, height: 800 });
  const localHtmlUrl = pathToFileURL(resolve('./index.html')).href;
  await page.goto(localHtmlUrl);
  await page.screenshot({ path: 'screenshot.png', fullPage: true });
} finally {
  await browser.close();
}

Install Puppeteer in your Node.js project first, following the official getting-started guide. The example assumes index.html is in the process’s current working directory. Change that path to your file.

As an Amazon Associate I earn from qualifying purchases.

Why convert the file path to a URL?

page.goto() navigates to a URL, and Puppeteer’s API requires the URL to include a scheme. pathToFileURL() converts a filesystem path into a suitable URL and handles URL-sensitive characters that can break a manually assembled file:// string. The Page.goto() reference documents navigation, and Node.js documents pathToFileURL in its URL API.

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

Choose the capture size and output

Viewport screenshot

Omit fullPage to capture the visible viewport. Set its dimensions with page.setViewport() before navigating or capturing when you need predictable output dimensions.

Full-page screenshot

Set fullPage: true to capture content beyond the viewport. It is false by default. Puppeteer’s ScreenshotOptions reference documents this and other capture settings.

File name and format

The path option saves the image to disk. A relative path such as screenshot.png is resolved from Node.js’s current working directory, which may differ from the HTML file’s directory. Use an absolute output path if the destination must be unambiguous. The extension determines the format unless you explicitly set a type; PNG is the default. See the ScreenshotOptions reference.

Use an HTML string instead of a file

If your input is already an HTML string, use page.setContent(html) rather than navigating to a file URL:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const html = '<!doctype html><html><body><h1>Hello</h1></body></html>';
await page.setContent(html);
await page.screenshot({ path: 'screenshot.png' });

The Page.setContent() API documents this method. It does not, by itself, establish how external local assets such as stylesheets, images, fonts, or scripts will resolve; verify those assets in your runtime.

Wait for JavaScript-rendered content

If the page adds content asynchronously, wait for a condition that signals the part you need is ready before calling page.screenshot(). For example, if the page renders a result element with a stable selector:

await page.goto(localHtmlUrl);
await page.waitForSelector('#rendered-result');
await page.screenshot({ path: 'screenshot.png', fullPage: true });

Choose a selector that appears only when the desired content is ready. There is no universal readiness rule for every local page; a fixed delay is less reliable than waiting for a page-specific signal when one exists.

Troubleshoot blank, incomplete, or misplaced captures

  • Blank or incomplete image: check that navigation has completed and that the expected CSS, images, fonts, and scripts loaded. Local-file access behavior can depend on the runtime and environment, so check the browser output and verify each referenced asset.
  • Content appears late or is missing: wait for the page-specific selector or other readiness signal before capturing.
  • Image has the wrong dimensions: set the viewport explicitly; use fullPage: true if you need content below the visible area.
  • Image is saved in the wrong folder: remember that a relative screenshot path starts at the Node.js process’s current working directory. Supply an absolute path to control the destination.
  • Paths with spaces or special characters fail: pass the filesystem path through pathToFileURL() instead of building the URL by concatenating strings.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

For a publicly reachable web page, ScreenshotNeo can return a screenshot with one GET request. This example saves the response as WebP:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
The SQL Programming Language: .
  • Used Book in Good Condition
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 documentation for the API details. ScreenshotNeo accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server offers screenshot and PDF tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. A local file on your computer is not a publicly reachable URL, so this API example is for a page accessible to the service, not a replacement for Puppeteer on a private local file. Sign up for 1,000 free screenshots a month, with no card required.

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.

HTMLNode.jsPuppeteerscreenshotsWeb development

Leave a Reply

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

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

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

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.