HTML to Image Command Line Code Examples
Use these command-line examples for common HTML to Image tasks. See the CLI guide for installation, input and output handling, and diagnostics.
You can run the examples below without account setup. The CLI uses demo credentials unless you have configured your own API credentials. Update the input URLs and filenames as needed.
Commands use POSIX-shell syntax, as in Bash or Zsh, and save results in the current directory.
Basic examples
These commands save the PNG with --output. See Handle the Result for output options and failure handling.
Convert a URL
html2image --output-format=png \ --output example.png \ https://example.com/
Convert an HTML file
Upload an existing document.html file. For local images and stylesheets, see Local assets.
html2image --output-format=png \ --output document.png \ document.html
Read HTML from stdin
Read HTML from an existing document.html file through stdin. Use the same - input when piping HTML from another program.
html2image --output-format=png \ --output html.png \ - < document.html
Convert inline HTML
Send HTML through stdin and save the PNG as report.png.
html2image --output-format=png \ --output report.png \ - <<'HTML' <!doctype html> <html><body><h1>Monthly report</h1></body></html> HTML
Local assets
Include HTML and its local images, stylesheets, and scripts in a ZIP archive, keeping their relative paths.
Convert a prepared archive
Upload an existing document.zip. Select the main HTML file explicitly when the archive contains multiple documents.
Detect the main HTML file
html2image --output-format=png \ --output output.png \ document.zip
Select index.html
html2image --output-format=png \ --zip-main-filename=index.html \ --output document.png \ document.zip
Convert a local directory
Use the zip utility to create document.zip from the existing document directory, then convert it to PNG.
(cd document && zip -q -r - .) > document.zip && html2image --output-format=png \ --output document.png \ document.zip
Page rendering
Set screenshot width and scale
Capture a 1280-pixel-wide viewport and scale the PNG output to 50%.
html2image --output-format=png \ --scale-factor=50 \ --screenshot-width=1280 \ --output customized.png \ https://example.com/
Hide or restyle page elements
Replace the selectors with elements from your page.
html2image '--custom-css= /* Hide navigation, ads, and sidebar */ nav, .advertisement, .sidebar, footer { display: none !important; } /* Adjust layout for screenshot */ .content { width: 100% !important; max-width: none !important; }' \ --output-format=png \ --output custom_css.png \ https://example.com/
Modify the page with JavaScript
Adjust the selectors and class names for your page.
html2image '--custom-javascript= // Remove dynamic content before conversion document.querySelectorAll('"'"'.video-player, .chat-widget'"'"').forEach(el => { el.remove(); }); // Expand collapsed sections document.querySelectorAll('"'"'.collapsed'"'"').forEach(el => { el.classList.remove('"'"'collapsed'"'"'); });' \ --output-format=png \ --output custom_js.png \ https://example.com/
Convert a specific element
Convert only #main in an existing document.html file. Adjust the filename and selector for your document.
html2image '--element-to-convert=#main' \ --output-format=png \ --output html_part.png \ document.html
Wait for a page element
Wait for #content-loaded to appear in an existing document.html file. Choose an element that appears when your content is ready.
html2image --output-format=png \ '--wait-for-element=#content-loaded' \ --output wait_element.png \ document.html
Allow extra time for JavaScript
Add a delay of 2000 milliseconds. When the page has a reliable readiness marker, use Wait for a page element.
html2image --javascript-delay=2000 \ --output-format=png \ --output with_delay.png \ https://example.com/
Apply print stylesheets
Apply the page’s @media print rules. The page must provide print styles for this to change its appearance.
html2image --output-format=png \ --use-print-media \ --output print_media.png \ https://example.com/
HTML and CSS
Add these fragments to HTML you control.
Remove or hide elements
<nav class="pdfcrowd-remove">This won't appear in the screenshot</nav> <div class="pdfcrowd-hide">This is invisible but takes up space</div>
pdfcrowd-remove removes the element from the layout. pdfcrowd-hide preserves its space.
Apply styles only during conversion
<style> /* These styles only apply during image conversion */ .pdfcrowd-body h1 { font-size: 48px; } .pdfcrowd-body footer { display: none; } .pdfcrowd-body .main-content { width: 100%; } </style>
The .pdfcrowd-body prefix limits these rules to conversion; it does not change the page in a visitor’s browser.
Templates
Render a template with JSON data
Upload existing template.html and data.json files. See the template rendering guide for template syntax, data mapping, and other supported data formats.
html2image --data-format=json \ --output-format=png \ --data-file=data.json \ --output output.png \ template.html