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

The right JavaScript method depends on what you mean by “image.” To turn an element or the current page into a downloadable canvas in the browser, use html2canvas. To capture a URL as a browser-rendered screenshot—especially from Node.js or a server—use Puppeteer and page.screenshot(). These approaches are not interchangeable: html2canvas reconstructs DOM content and styles, while Puppeteer captures a page through an actual browser.

Choose the right capture method

What you need Use Important trade-off
Capture one element on the page already open html2canvas(element) Simple browser integration, but it reconstructs supported DOM and CSS rather than taking a literal pixel screenshot.
Capture the current document html2canvas(document.body) Convenient for an open page; cross-origin resources and canvas-size limits can affect the result.
Capture a URL in a browser or server workflow Puppeteer page.screenshot() Produces browser screenshot bytes, but requires a browser-automation environment.
Capture URLs without operating browser infrastructure Hosted screenshot API Service behavior, limits and terms vary; check the provider’s documentation.

Capture an HTML element with html2canvas

html2canvas’s getting-started guide documents a Promise-returning browser call. Install the package with your package manager, then run code in a page that has window and document.

import html2canvas from '@html2canvas/html2canvas';

const element = document.querySelector('#capture');
if (!element) throw new Error('Capture element not found');

const canvas = await html2canvas(element);
const link = document.createElement('a');
link.download = 'website.png';
link.href = canvas.toDataURL('image/png');
link.click();

The selector guard turns a silent failure into a useful error. Add an element such as <section id="capture">...</section> to the page. The returned Promise resolves to a canvas; you can append that canvas to the document, draw it elsewhere, or export it.

Export JPEG or WebP

PNG is lossless and supports transparency. For a smaller photographic image, request another MIME type when converting the canvas:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
const blobUrl = canvas.toDataURL('image/jpeg', 0.9);
const link = document.createElement('a');
link.download = 'website.jpg';
link.href = blobUrl;
link.click();

Browser support and encoding behavior differ by format. If a format is not supported, the canvas export may fall back to PNG; verify the output in the target browsers.

Capture the whole current page

Pass document.body when the requirement is the page currently open in the user’s browser:

const canvas = await html2canvas(document.body, {
  useCORS: true,
  scale: window.devicePixelRatio
});

document.body.appendChild(canvas);

scale can improve sharpness on high-density displays, but it multiplies pixel dimensions and memory use. A larger scale is not automatically better for very long pages.

Crop to a region

The project’s examples show region coordinates and dimensions. Cropping is useful when a page is too large or only a viewport section matters:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const canvas = await html2canvas(document.body, {
  x: 0,
  y: 0,
  width: 1200,
  height: 800,
  scale: 1
});

Hide controls or other elements

You can omit elements such as sticky toolbars before rendering by using the library’s ignore controls. The exact option shape can change between releases, so check the current documentation for the version you install. Another practical approach is to add a temporary class that sets display:none, capture, and then remove the class.

What html2canvas can and cannot reproduce

html2canvas traverses the DOM and styles and builds its own canvas representation. It is therefore not a pixel-identical screenshot mechanism. CSS properties that the library does not implement, browser-native effects, and content rendered outside the accessible DOM may be missing or look different. The official documentation lists these limitations.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
  • Use it when you control the page and need an image of DOM content.
  • Do not promise exact browser pixels for every CSS feature.
  • Capture after fonts, images and asynchronous content have finished loading.
  • For a server process or an arbitrary URL, use browser automation instead.

Handle cross-origin images and frames

Images loaded from another origin are governed by browser content policy. Setting useCORS: true only helps when the remote server sends suitable CORS headers; it cannot grant permission by itself. Otherwise an image may be skipped or the canvas may become tainted, preventing export. The FAQ describes the CORS and proxy options.

const canvas = await html2canvas(element, {
  useCORS: true
});

try {
  const png = canvas.toDataURL('image/png');
} catch (error) {
  console.error('Canvas export blocked by cross-origin content', error);
}

If you control the image server, configure it to allow the requesting origin and ensure the image request uses CORS mode. If you do not control it, a properly designed server-side proxy may be required; the browser library cannot override the policy.

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

Cross-origin iframe documents are also inaccessible because browser security prevents reading their DOM. Same-origin frames are supported when your page has permission to access the frame document. A screenshot service or browser automation can capture the rendered frame as part of the page without giving your JavaScript direct access to its document.

Canvas size and long-page failures

Canvas dimensions have implementation limits that vary by browser, operating system and hardware. Very tall pages can produce a blank or partially rendered canvas. The html2canvas FAQ recommends treating these limits as variable rather than relying on one universal maximum.

  • Capture a bounded region instead of the entire document.
  • Split a long page into sections and stitch the resulting images in a separate process.
  • Reduce scale when memory usage is high.
  • Remove unnecessary content and retry after images have loaded.

Capture a URL with Puppeteer

When the requirement is a browser-rendered screenshot of a URL, Puppeteer is the closer match. It launches or connects to a browser, navigates to the page, and returns image bytes from page.screenshot(). The API documentation is on the project’s main branch, so confirm details against the version in your project.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
import puppeteer from 'puppeteer';

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

fullPage: true asks Puppeteer to capture the full document. Omit it for a viewport shot. In production, set navigation and screenshot timeouts appropriate to your pages and always close the browser in a finally block.

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

Return bytes instead of writing a file

const bytes = await page.screenshot({ type: 'png' });
// bytes is a Buffer in Node.js; send it in an HTTP response or store it.

This is useful for an API endpoint that streams the image directly to a client. Choose JPEG or WebP when your delivery pipeline and target browsers support them.

Timing, dynamic content and reproducibility

Neither method automatically knows when an application is visually complete. With html2canvas, wait for data, fonts and images in your own page before calling it. With Puppeteer, select a navigation condition such as networkidle2, then add an application-specific wait for a selector when necessary:

await page.goto(url, { waitUntil: 'domcontentloaded' });
await page.waitForSelector('#report', { timeout: 30000 });
await page.screenshot({ path: 'report.png' });

Pages with continuously polling analytics or live updates may never become truly idle. In those cases, wait for a meaningful selector or a deliberate short delay rather than an indefinite network-idle condition.

Troubleshooting checklist

The selector is null

Cause: the element is absent or the script ran before the page created it. Fix: verify the selector in DevTools and run after the component mounts; keep the explicit guard so the failure is visible.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

Images are missing or export throws a security error

Cause: cross-origin images lack permission. Fix: configure CORS on the image host, use an approved proxy, or switch to browser automation for a URL screenshot. useCORS alone cannot bypass policy.

An iframe is blank

Cause: it is cross-origin. Fix: capture content you control in the same origin, or capture the rendered page with Puppeteer or a hosted service.

The output is blank or cut off

Cause: canvas dimensions or memory limits. Fix: crop, lower the scale, or split a long page into sections.

Fonts or late content are absent

Cause: capture started before resources finished. Fix: wait for your data and fonts in the browser, or wait for a specific selector in Puppeteer.

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

The Node.js script fails because window is undefined

Cause: html2canvas depends on browser APIs and is not intended for Node.js server rendering. Use Puppeteer for server-side capture.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
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 hosted website screenshot API and MCP server. One GET request returns PNG, JPEG, WebP or PDF. Before capture it accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups and chat widgets; each cleanup step can be disabled. Bot checks, CAPTCHAs, blank pages, timeouts, failed loads and cache hits are not billed, and response headers report the page verdict and billing result.

Use the API directly when you do not want to maintain Chromium:

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

Equivalent Python:

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

Equivalent 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}`);
const image = Buffer.from(await res.arrayBuffer());

See the ScreenshotNeo documentation for parameters. It supports full-page and selector captures, device presets, custom viewports, retina scale, dark mode, PDF settings, custom CSS and JavaScript, clicks, waits, blocking rules, headers, cookies, user agents, authorization, timezone, geolocation, transparent backgrounds, resizing, chosen cache TTLs, signed image links, asynchronous webhooks, bulk capture of up to 100 URLs per call, usage data and an OpenAPI specification. Its MCP server exposes take_screenshot, get_page_info and capture_pdf to Claude, Cursor and other MCP clients.

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

Every feature is available on every plan: 1,000 shots per month free with no card, Starter is $5 for 3,000, Growth $15 for 15,000, Pro $39 for 60,000, Scale $99 for 250,000, and Business $249 for 1,000,000. Yearly billing gives two months free. Start with the free ScreenshotNeo account.

Which approach should you use?

  • Choose html2canvas for an element or document already open in your browser, when DOM-level reconstruction is acceptable.
  • Choose Puppeteer when you need browser pixels, arbitrary URLs, server-side execution or access to a page’s rendered state.
  • Choose ScreenshotNeo when you want URL capture without running browser infrastructure, with cleanup of common overlays and billing that excludes failed captures.

Frequently Asked Questions

Can JavaScript save a webpage directly as PNG?

Yes. In a browser, render the target with html2canvas, call canvas.toDataURL(‘image/png’), and trigger an anchor download. For a URL captured on a server, use Puppeteer or a hosted screenshot API.

Does html2canvas capture a cross-origin iframe?

No. Browser security blocks access to a cross-origin frame’s document. Same-origin frames can be rendered when your page has access.

Is html2canvas a real screenshot?

No. It reconstructs DOM content and supported styles into a canvas, so unsupported CSS and browser effects may differ from the displayed pixels.

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.

Can I run html2canvas in Node.js?

Not as documented: it depends on browser APIs such as window and document. Use Puppeteer or a hosted service for Node.js capture.

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.