Skip to main content

通过浏览器渲染图片

Project description

nonebot-plugin-htmlrender

  • 通过浏览器渲染图片
  • 可通过查看example参考使用实例
  • 如果有安装浏览器等问题,先查看文档最底下的常见问题再去看 issue 有没有已经存在的

✨ 功能

  • 通过 html 和浏览器生成图片
  • 支持纯文本 markdownjinja2 模板输入
  • 通过 CSS 来控制样式

使用

参考example/plugins/render/init.py

from nonebot import require
require("nonebot_plugin_htmlrender")
# 注意顺序,先require再 from ... import ...
# 注意顺序,先require再 from ... import ...
# 注意顺序,先require再 from ... import ...
from nonebot_plugin_htmlrender import (
    text_to_pic,
    md_to_pic,
    template_to_pic,
    get_new_page,
)
# 注意顺序,先require再 from ... import ...
# 注意顺序,先require再 from ... import ...
# 注意顺序,先require再 from ... import ...

配置

# 默认情况 可不写
htmlrender_browser = "chromium"
# 使用 firefox
htmlrender_browser = "firefox"

# 下载 playwright 代理地址 可不写
htmlrender_download_host = ""

# 浏览器自定代理地址 可不写
htmlrender_proxy_host = "http://127.0.0.1:7890"

# 浏览器 channel 支持以下
# "chrome", "chrome-beta", "chrome-dev", "chrome-canary",
# "msedge", "msedge-beta", "msedge-dev", "msedge-canary"
# 手动编辑可以直接使用系统自带浏览器而不用重新下载 chromium
# 可不写
htmlrender_browser_channel = ""

markdown 转 图片

  • 使用 GitHub-light 样式
  • 支持绝大部分 md 语法
  • 代码高亮
  • latex 数学公式 (感谢@MeetWq
    • 使用 $$...$$ 来输入独立公式
    • 使用 $...$ 来输入行内公式
  • 图片需要使用外部连接并使用html格式 否则文末会超出截图范围
  • 图片可使用 md 语法 路径可为 绝对路径(建议), 或 相对于template_path 的路径

模板 转 图片

  • 使用 jinja2 模板引擎
  • 页面参数可自定义

🌰 栗子

example.md

文本转图片(同时文本里面可以包括 html 图片)

markdown 转图片(同时文本里面可以包括 html 图片)

纯 html 转图片

jinja2 模板转图片

特别感谢

  • MeetWq 提供数学公式支持代码和代码高亮

常见疑难杂症

playwright._impl._api_types.Error: 初次运行时报错

  • 一般为缺少必要的运行环境,如中文字体等

Ubuntu 使用 apt

apt update && apt install -y locales locales-all fonts-noto libnss3-dev libxss1 libasound2 libxrandr2 libatk1.0-0 libgtk-3-0 libgbm-dev libxshmfence1
  • 然后设置 ENV local
LANG zh_CN.UTF-8
LANGUAGE zh_CN.UTF-8
LC_ALL zh_CN.UTF-8

CentOS 使用 yum

  • 小心 CentOS
  • 参考CentOS Dockerfile
  • 添加中文字体库
  • 最佳解决办法
    • 使用 Docker 然后用 Python 镜像 按照上面 Ubuntu 的写 dockerfile

下面这个依赖运行一下 也许就可以用了

dnf install -y alsa-lib at-spi2-atk at-spi2-core atk cairo cups-libs dbus-libs expat flac-libs gdk-pixbuf2 glib2 glibc gtk3 libX11 libXcomposite libXdamage libXext libXfixes libXrandr libXtst libcanberra-gtk3 libdrm libgcc libstdc++ libxcb libxkbcommon libxshmfence libxslt mesa-libgbm nspr nss nss-util pango policycoreutils policycoreutils-python-utils zlib cairo-gobject centos-indexhtml dbus-glib fontconfig freetype gtk2 libXcursor libXi libXrender libXt liberation-fonts-common liberation-sans-fonts libffi mozilla-filesystem p11-kit-trust pipewire-libs harfbuzz-icu libglvnd-glx libglvnd-egl libnotify opus woff2 gstreamer1-plugins-base gstreamer1-plugins-bad-free openjpeg2 libwebp enchant libsecret hyphen libglvnd-gles

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonebot_plugin_htmlrender-0.3.1.tar.gz (1.0 MB view hashes)

Uploaded Source

Built Distribution

nonebot_plugin_htmlrender-0.3.1-py3-none-any.whl (1.0 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page