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

To convert a webpage to PNG in Rust, use a browser engine such as Chromium, control it with the headless_chrome crate, wait until the page is ready, then write the screenshot bytes to a file. This produces a browser-rendered image rather than an image of the raw HTML source. The example below captures the current page viewport; full-page and element captures require choosing the appropriate capture bounds or element API.

Use headless Chrome from Rust

The headless_chrome crate documentation shows version 1.0.22 and an API for opening a browser tab, navigating to a URL, waiting for an element, and capturing PNG bytes. The crate controls Chrome or Chromium through the DevTools Protocol. The application still needs an available compatible browser binary unless you configure the crate’s optional browser-fetching feature.

1. Add the dependency and install a browser

Add headless_chrome to your project’s Cargo.toml:

[dependencies]
headless_chrome = "1.0.22"

Install Chrome or Chromium in the environment where the program will run. For repeatable deployments, install and pin the browser version separately so an OS package update does not silently change rendering behavior. The crate project documents an optional fetch feature for downloading known-good Chromium binaries; review its project documentation for the configuration supported by the crate version you select.

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.

2. Navigate, wait, capture, and save

This runnable example loads a URL, waits for the document body, captures a PNG, and writes it to output.png in the current working directory:

use headless_chrome::{protocol::cdp::Page, Browser};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let browser = Browser::default()?;
    let tab = browser.new_tab()?;

    tab.navigate_to("https://example.com")?;
    tab.wait_for_element("body")?;

    let png = tab.capture_screenshot(
        Page::CaptureScreenshotFormatOption::Png,
        None,
        None,
        true,
    )?;
    std::fs::write("output.png", png)?;
    Ok(())
}

The wait_for_element("body") check confirms that the body exists, not that every application-specific task has finished. If a page fills in content after initial load, wait for a selector that appears only when the relevant content is ready, or use the crate’s JavaScript interaction capabilities to check an application-specific condition. Remote images and web fonts can also arrive after the initial document markup, so verify readiness against the actual assets your output depends on.

Convert HTML you already have

navigate_to takes a URL; it does not take an HTML string. If your Rust program builds markup in memory, make it available to a browser as a page before capturing it. Two practical approaches are to serve it from a local HTTP server and navigate to that local URL, or to load it through a properly encoded data URL. A local server is usually easier when the page references relative assets, needs custom response headers, or contains content large enough to make URL encoding awkward.

For a local page, keep the server running until navigation and capture finish, and make sure referenced stylesheets, scripts, fonts, and images are reachable from the browser. A data URL is self-contained only when the markup and its required resources are included or otherwise accessible; relative asset paths may not resolve as expected without a suitable base URL.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Choose page, element, and viewport bounds

Viewport screenshot

The code above captures a page screenshot using the crate’s documented screenshot API. A predictable viewport matters when the output is used for visual comparisons or fixed-size graphics. Set the browser viewport deliberately using the configuration supported by your selected crate version, and keep that configuration consistent across runs. The available sources show screenshot capture with bounds-related parameters, but do not establish a universal full-page configuration or exact pixel dimensions for every page.

Capture one element

When you only need a chart, card, or other component, wait for the target selector and use the element API’s screenshot method rather than capturing the whole tab. The documented element capture accepts the PNG format option. This reduces unrelated page content in the output, but the selected element must exist and be laid out before capture.

Full-page output

Do not assume that a viewport capture automatically includes the entire document. Full-page capture depends on the browser protocol options and the crate API parameters used. Confirm those parameters for the crate version and browser you deploy, then check the output for clipped content, lazy-loaded images, and unexpected page breaks or scaling.

Wait for dynamic content before capturing

A successful navigation only says the browser reached a page; it does not guarantee that a single-page application has rendered its final state. Prefer a page-specific readiness signal over an arbitrary pause:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.
  • Wait for a selector that is rendered only after the content you need appears.
  • For pages you control, expose a clear ready marker after data and layout are complete.
  • When remote images or fonts affect the result, verify they have loaded before capture.
  • Use a fixed delay only when the page offers no better signal, and choose it based on observed behavior in your own environment.

Waiting for body is a useful minimal example, but it can capture a mostly empty shell on a client-rendered page. Treat readiness checks as part of the conversion logic, not as an optional polish step.

When the crate is not the right fit

headless_chrome is synchronous and does not implement every Puppeteer or DevTools Protocol feature. If your Rust application requires asynchronous WebDriver automation or support for browsers beyond Chrome, the project points to fantoccini as an alternative. Choose based on the browser you need, whether your application is async, how much automation control is required, and how you will manage browser installation and versions.

Use Chromium’s command line instead

For a one-off screenshot, Chromium can render a URL without Rust code driving the capture. Chrome Developers documents this command:

chrome --headless --disable-gpu --screenshot --window-size=1280,900 https://example.com

The documented default output is screenshot.png in the current working directory. Adjust the window size to the viewport you need. This route is simple, but selector waits, page-specific JavaScript checks, and element-only capture are easier to orchestrate through a browser automation API. The Chromium documentation notes that full-page screenshots require additional steps.

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

A Rust program can also spawn Chromium as a child process when all it needs is a command-line capture. For lower-level control, Chromium’s headless README describes launching with --headless --remote-debugging-port=9222 and controlling the browser through the DevTools Protocol.

Check Chromium’s headless mode and deployment setup

Browser packaging and headless behavior can vary by installed version. Chromium’s current headless README says that, as of M132, the old headless functionality is no longer part of the Chrome binary, --headless=old has no effect, and users of the old Headless implementation should migrate to chrome-headless-shell. Check the installed binary and current migration guidance rather than assuming that a command copied from an older environment still selects the same implementation.

In a deployment environment, account for browser installation, version pinning, process lifetime, and access to the page’s network resources. A browser process can fail even when the Rust code compiles; validate the complete browser-plus-application setup in the target container or host. No single configuration guarantees identical output across different browser builds, operating systems, fonts, or page states.

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

Troubleshoot common capture problems

Symptom Likely cause What to check
Browser startup fails Chrome or Chromium is missing, inaccessible, or incompatible with the crate’s expected setup. Confirm the binary is installed in the runtime environment. If using the optional fetch feature, confirm it is enabled and configured for the crate version in use.
Screenshot is blank or incomplete The page navigated, but application content or remote assets were not ready. Wait for a meaningful selector or application-ready condition; check that images, stylesheets, and scripts are reachable from the browser.
Element capture cannot find its target The selector is wrong, the element has not rendered, or the page is in a different state than expected. Verify the selector against the rendered DOM and wait for that element before requesting its screenshot.
Output is clipped The capture covers the viewport rather than the full document, or the chosen bounds do not match the intended output. Review the crate’s capture parameters and browser configuration; test the result with the actual page and dimensions.
Legacy headless command stops working The installed Chromium version may no longer include old headless functionality. Check the M132 migration guidance and use the current documented headless mode or chrome-headless-shell where required.
Local HTML appears without its styling or assets Relative paths or local resources are unavailable to the browser at the page’s loaded URL. Serve the HTML and assets from a local server with valid paths, or make the data URL content self-contained and its resources accessible.

Or skip the browser setup

ScreenshotNeo provides a screenshot API and MCP server for developers. A single GET request can return a PNG, JPEG, WebP, or PDF. For a PNG capture of a URL, use:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.png

See the ScreenshotNeo API documentation for request options and response details. ScreenshotNeo accepts cookie or consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and the response identifies the page verdict and billing status in headers. An MCP server exposes take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Every feature is available on every plan.

ScreenshotNeo is made by Yorker Media. Sign up free for 1,000 screenshots a month with no card.

Frequently Asked Questions

Does this create a PNG from HTML source without rendering a browser?

No. The method renders the page in Chrome or Chromium and saves the browser’s screenshot as PNG bytes.

Can I use headless_chrome with an async Rust application?

The crate is synchronous. If asynchronous WebDriver automation is a requirement, evaluate the project’s cited fantoccini alternative.

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

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.