html-to-image-render
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHTML 转图片
HTML to Image
当任务是把本地 文件通过远端 API 渲染成图片时,使用这个 skill。
.htmlhtml2imageWhen the task is to render local files into images via the remote API, use this skill.
.htmlhtml2image工作流
Workflow
- 从磁盘读取 HTML 文件内容。
- 如有必要,先查看 中的接口约定。
references/html-to-image-api.md - 运行 ,传入输入 HTML 路径和输出图片路径。
scripts/render-html-to-image.ts - 确认生成的图片文件已经落到目标位置。
- Read the content of the HTML file from disk.
- If necessary, check the interface conventions in first.
references/html-to-image-api.md - Run , passing in the input HTML path and output image path.
scripts/render-html-to-image.ts - Confirm that the generated image file has been saved to the target location.
脚本说明
Script Description
内置脚本会完成这些事情:
- 以 UTF-8 读取 HTML 文件
- 调用
POST https://html2image.hanfangyuan.cn/api/html2image - 解码返回的
image_base64 - 把图片写入本地磁盘
示例:
bash
npx -y tsx skills/html-to-image-render/scripts/render-html-to-image.ts \
--input ./demo/card.html \
--output ./out/card.png \
--element-id card \
--image-type png执行这个脚本依赖本机可用的 ,并通过 临时执行 TypeScript 文件。首次运行时通常会下载 。
npxnpx -y tsxtsxThe built-in script will complete the following tasks:
- Read HTML files in UTF-8 encoding
- Call
POST https://html2image.hanfangyuan.cn/api/html2image - Decode the returned
image_base64 - Write the image to local disk
Example:
bash
npx -y tsx skills/html-to-image-render/scripts/render-html-to-image.ts \
--input ./demo/card.html \
--output ./out/card.png \
--element-id card \
--image-type pngRunning this script depends on the availability of on the local machine, and temporarily executes TypeScript files via . is usually downloaded on the first run.
npxnpx -y tsxtsx参数说明
Parameter Description
必选参数
Required Parameters
- :输入的本地 HTML 文件路径。
--input - :输出图片文件路径。
--output
- : Path of the input local HTML file.
--input - : Path of the output image file.
--output
可选参数
Optional Parameters
- :要截图的元素 id,传原始 id 值,不要带
--element-id。# - :支持
--image-type和png。jpeg - :设备像素比,越大越清晰,但图片通常也更大。
--device-scale-factor - /
--viewport-width:渲染视口尺寸。--viewport-height - :截图前额外等待时间,适合等待字体、图片等异步资源。
--render-wait-ms - :当指定
--element-padding时,为截图区域额外增加的边距,默认值为--element-id。0
- : The id of the element to be captured, pass the original id value without
--element-id.# - : Supports
--image-typeandpng.jpeg - : Device pixel ratio, the higher the value, the clearer the image, but the larger the image size usually is.
--device-scale-factor - /
--viewport-width: Render viewport size.--viewport-height - : Extra waiting time before taking a screenshot, suitable for waiting for asynchronous resources such as fonts and images.
--render-wait-ms - : When
--element-paddingis specified, the extra margin added to the screenshot area, the default value is--element-id.0
注意事项
Notes
- 运行环境需要可用的 。
npx - 如果 HTML 中使用图片资源,需要使用公网可访问的图片链接,不支持本地图片路径。
- 输出文件扩展名应与 保持一致。
--image-type - 要注意实际截图区域的尺寸是否能完整容纳目标 HTML。尤其当卡片宽高大于默认视口时,必须显式调整 和
--viewport-width,避免截图被裁切或图片不完整。--viewport-height - 只有在默认渲染结果不满足需求时,再调整视口和等待时间参数;但如果 HTML 的设计尺寸本身就大于默认视口,应该优先先把视口调到不小于目标元素尺寸。
- 如果接口返回 ,通常是
400对应元素不存在。element_id - 如果接口返回 ,通常是参数范围不合法。
422
- The running environment requires available .
npx - If image resources are used in HTML, publicly accessible image links are required, and local image paths are not supported.
- The output file extension should be consistent with .
--image-type - Pay attention to whether the size of the actual screenshot area can fully accommodate the target HTML. Especially when the width and height of the card are larger than the default viewport, you must explicitly adjust and
--viewport-widthto avoid cropped or incomplete screenshots.--viewport-height - Adjust the viewport and waiting time parameters only when the default rendering result does not meet the requirements; but if the design size of the HTML itself is larger than the default viewport, you should first adjust the viewport to be no smaller than the size of the target element.
- If the interface returns , usually the element corresponding to
400does not exist.element_id - If the interface returns , usually the parameter range is invalid.
422