← 返回
未分类

HTML DOM To Image

Convert HTML DOM nodes to images (PNG/JPEG/SVG/Blob). Supports rendering options such as filter, backgroundColor, quality, and pixelRatio.
将 HTML DOM 节点转换为图像(PNG/JPEG/SVG/Blob),支持滤镜、背景色、质量、像素比等渲染选项。
openlark
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 294
下载
💾 0
安装
1
版本
#latest

概述

HTML DOM To Image

Convert any HTML DOM node into an image in PNG, JPEG, SVG, or Blob format.

Use Cases

Users request to convert HTML content to an image, take a screenshot of a web page element, generate a PNG/JPEG cover image, or export a specified DOM node as an image file.

Installation

Install html-to-image in the project directory:

npm install html-to-image

Core Workflow

Step 1: Target the DOM Node

Get the HTML element to convert using a selector:

const node = document.querySelector('#target-element');

Step 2: Choose Output Format

FormatFunctionDescription
-------------------------------
PNGtoPng(node, options)Default format, transparency preserved
JPEGtoJpeg(node, { quality: 0.95 })Compressible, suitable for photos
SVGtoSvg(node, { filter })Vector format
BlobtoBlob(node)Binary format, suitable for file downloads

Step 3: Trigger Download

htmlToImage.toPng(node, { cacheBust: true })
  .then((dataUrl) => {
    const link = document.createElement('a');
    link.download = 'my-image.png';
    link.href = dataUrl;
    link.click();
  })
  .catch((err) => console.error('oops, something went wrong!', err));

Common Rendering Options

OptionTypeDescription
---------------------------
backgroundColorstringCSS color value, e.g. '#ffffff'
width, heightnumberPixel dimensions to apply to the node before rendering
canvasWidth, canvasHeightnumberScaling dimensions for the canvas
qualitynumberJPEG quality 0~1, default 1.0
cacheBustbooleanAppend a timestamp to disable caching, default false
pixelRationumberPixel ratio; defaults to the device's actual pixel ratio; set to 1 to render at 1x initial scale
filterfunction(node) => boolean, returns true to keep the node

filter Example (Exclude Specific Elements)

const filter = (node) => {
  const exclude = ['remove-me', 'secret-div'];
  return !exclude.some(c => node.classList?.contains(c));
};

htmlToImage.toJpeg(node, { quality: 0.95, filter });

React Integration

import { useCallback, useRef } from 'react';
import { toPng } from 'html-to-image';

const ExportButton = () => {
  const ref = useRef<HTMLDivElement>(null);

  const onClick = useCallback(() => {
    if (!ref.current) return;
    toPng(ref.current, { cacheBust: true })
      .then((dataUrl) => {
        const link = document.createElement('a');
        link.download = 'my-image.png';
        link.href = dataUrl;
        link.click();
      })
      .catch(console.error);
  }, []);

  return (
    <>
      <div ref={ref}>
        {/* Content to convert */}
      </div>
      <button onClick={onClick}>Download Image</button>
    </>
  );
};

Notes

  • Including a CORS-tainted will cause rendering to fail
  • Extremely large DOM nodes may fail due to dataURI length limits
  • IE browser is not supported (incompatible with SVG )

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-08 03:32 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

Text Summarizer

openlark
抽取式AI文本摘要工具,自动使用TextRank+TF‑IDF混合算法从任意文本中提取最重要的句子。
★ 0 📥 693

Sqlite Client

openlark
SQLite 数据库操作技能。当用户需要创建、读取、查询或修改 SQLite 数据库(.db 文件)时使用。
★ 0 📥 657

Toutiao Graphic Publisher

openlark
通过浏览器自动化在头条发布图文内容,支持智能排版、自动生成热门标签等功能。
★ 2 📥 925