entari-plugin-htmlrender
面向 Entari 的可插拔图片渲染插件:支持将 HTML、Markdown、纯文本、Jinja2 模板 渲染为图片,同时内置基于 Pillow / Skia 的独立 Python 绘图能力。
在线文档 · 快速开始 · 配置参考 · API 参考 · 示例项目 · 升级到 0.2
功能概览
- 统一的内容出图接口:通过
HtmlRenderer一致地渲染 HTML、Markdown、纯文本与 Jinja2 模板,也支持用HtmlPreparer将“内容准备”与“后端出图”拆开复用。 - 可切换的 HTML 渲染后端:
- Takumi:基于 Rust 的进程内原生排版引擎,轻量快速,额外提供 SVG 输出、布局测量、动图编码与动态字体管理。
- Playwright:支持远程 WebSocket / CDP 连接与本地启动,完整支持动态网页、选择器截图与 KaTeX 数学公式。
- 独立的 Python 绘图通道:无需启用任何 HTML 引擎,即可直接用 Pillow 或 Skia 编写同步绘图函数(支持按文件/模块组织与开发期热重载),或渲染声明式
RasterScene。 - 默认安全的资源边界:本地文件读取采用显式路径白名单(含符号链接校验),远程资源拉取默认拦截私网地址并逐跳校验重定向。
快速开始
运行环境要求:Python >=3.11,<4.0,Entari >=0.19.4,<0.20.0。
1. 安装插件与所需后端
基础包不包含任何渲染后端,请按实际场景选择可选依赖(可组合安装,如 [takumi,pillow]):
# 推荐:使用 Takumi 渲染静态内容(无需安装浏览器)
uv add "entari-plugin-htmlrender[takumi]>=0.2.0"
# 需要完整浏览器能力(JavaScript、网页截图、数学公式)
uv add "entari-plugin-htmlrender[playwright]>=0.2.0"
# 需要 Python 绘图能力(按需选择 pillow 或 skia)
uv add "entari-plugin-htmlrender[pillow]>=0.2.0"
2. 配置 Entari
在 Entari 配置文件中启用 htmlrender:
plugins:
htmlrender:
provider: takumi
startup: probe
3. 在插件中调用
在事件处理函数中通过 Entari 依赖注入直接声明 HtmlRenderer(支持注入 HtmlRenderer、HtmlPreparer、TemplateRenderer、ResourceAccess、GraphicsRenderer、DrawingRenderer 或聚合入口 HtmlRenderService):
from entari_plugin_htmlrender import HtmlRenderer, RasterOptions, RenderedImage
async def render_card(renderer: HtmlRenderer) -> RenderedImage:
return await renderer.rasterize_markdown(
"# Hello, Entari\n\nRendered with Takumi.",
raster=RasterOptions(width=720),
)
返回的 RenderedImage 可直接通过 bytes(image) 提取 PNG / JPEG 字节流,并通过 image.width、image.height、image.media_type 读取元信息。HTML、纯文本与模板分别对应 rasterize_html()、rasterize_text() 和 rasterize_template()。
渲染与绘图方案对比
| 方案 | 可选依赖 | 适用场景 | 主要边界 |
|---|---|---|---|
| Takumi | [takumi] |
静态卡片、帮助菜单、Markdown、Jinja2 模板、SVG、测量与动图 | 不执行 JavaScript;不支持 Markdown 数学公式;样式需内联 |
| Playwright | [playwright] |
复杂浏览器布局、JavaScript、网页/选择器截图、KaTeX 数学公式 | 需运行浏览器实例(生产环境推荐独立部署远程服务) |
| Pillow / Skia | [pillow] / [skia] |
使用 Python 函数直接绘制卡片、拼图、矢量图形或 RasterScene |
独立于 HTML 引擎,不解析 HTML / Markdown |
其他可选依赖:[filehost](远程浏览器本地大文件托管)、[prometheus](Prometheus 指标)、[sentry](Sentry 追踪)、[all](安装全部可选组件)。
示例与文档
- 示例项目
- 深入阅读
- 如何选择渲染方案 · Python 绘图指南 · 生产最佳实践 · 故障排查 · 参与贡献
致谢
本项目基于开源社区的多项优秀工作构建与演进,在此一并致谢:
- 上游项目
- kexue-z/nonebot-plugin-htmlrender:本项目最初移植并演进自 NoneBot 生态的
nonebot-plugin-htmlrender,感谢上游作者与贡献者奠定的设计与实践基础。
- kexue-z/nonebot-plugin-htmlrender:本项目最初移植并演进自 NoneBot 生态的
- 框架与运行时生态
- ArcletProject/Entari、ArcletProject/Launart 与 ArcletProject/Letoderea:提供插件宿主、服务生命周期编排与依赖注入支持。
- agronholm/anyio 与 aio-libs/aiohttp:提供异步并发原语与内置 Filehost HTTP 服务。
- 渲染与图形引擎
- kane50613/takumi 与 BalconyJH/takumi-py:提供高性能的 Rust 进程内排版渲染能力与 Python 绑定。
- microsoft/playwright-python:提供本地与远程浏览器渲染及页面自动化支持。
- python-pillow/Pillow 与 kyamagu/skia-python:提供 Python 2D 光栅与矢量绘图后端。
- 内容准备、样式与文档工具链
- pallets/jinja、Python-Markdown/markdown、facelessuser/pymdown-extensions 与 pygments/pygments:提供模板引擎、Markdown 扩展解析与代码语法高亮。
- KaTeX/KaTeX 与 sindresorhus/github-markdown-css:提供内置数学公式排版与默认 Markdown 样式。
- zensical/zensical、mkdocstrings/mkdocstrings 与 squidfunk/mike:提供文档站点构建、API 参考自动生成与多版本文档管理。
开源协议
本项目基于 MIT License 开源。
Metadata
Release files for entari-plugin-htmlrender 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| entari_plugin_htmlrender-0.2.0.tar.gz | 2.8 MB | With provenance | Details for entari_plugin_htmlrender-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| entari_plugin_htmlrender-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for entari_plugin_htmlrender-0.2.0-py3-none-any.whl |
Total release size: 4.1 MB
Release files / entari_plugin_htmlrender-0.2.0.tar.gz
| Download URL | entari_plugin_htmlrender-0.2.0.tar.gz |
|---|---|
| Size | 2.8 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7877764ef398112de9d895e0abc7f2f1202cda9d1b3d5e4e80a4a10e53c76936
|
|
BLAKE2b-256 checksum How to use checksums |
0ebe75f7db72b6c9cc06f0116f8910df4a7b2d9ebd99ffae69392bb0138a2f22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logRelease files / entari_plugin_htmlrender-0.2.0-py3-none-any.whl
| Download URL | entari_plugin_htmlrender-0.2.0-py3-none-any.whl |
|---|---|
| Size | 1.3 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d02ab343e8a721b8a4ad2f1a75d02af20b0da100a161baafc5fce07addd05f62
|
|
BLAKE2b-256 checksum How to use checksums |
6dc9f324cc930adfbee7724b3ea4bc12aefff7d919ce19e8983f66b3c42c4c9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logSecurity for entari-plugin-htmlrender 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for entari_plugin_htmlrender-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.