Skip to main content

Lightweight HTML rendering library based on litehtml

Project description

htmlkit

一个基于 litehtml 的轻量级 HTML 渲染库。

原始代码

nonebot/plugin-htmlkit

特性

  • 基于 fontconfig 的字体管理, 支持系统字体和自定义字体
  • 提供了 HTML,纯文本,markdown,和 Jinja2 模板渲染的快捷函数
  • 支持自定义图片和 CSS 的加载策略
  • 支持通过 CSS 控制样式
  • 支持自适应控制渲染宽度

安装

使用 pip 安装:

pip install pyhtmlrender

或者使用其他 Python 包管理器安装。

使用

快速开始

import asyncio
from htmlkit import html_to_pic, text_to_pic, md_to_pic

async def main():
    # 渲染 HTML 到图片
    html = "<h1>Hello World</h1><p>This is a test.</p>"
    image_bytes = await html_to_pic(html)
    
    # 保存图片
    with open("output.png", "wb") as f:
        f.write(image_bytes)

# 运行
asyncio.run(main())

初始化 Fontconfig

在使用前,建议先初始化 fontconfig:

from htmlkit import init_fontconfig

# 使用默认配置初始化
init_fontconfig()

# 或者指定自定义配置
init_fontconfig(
    fontconfig_path="/path/to/fonts",
    fontconfig_file="/path/to/fonts.conf"
)

API

  • html_to_pic(html, **options) - 将 HTML 渲染为图片
  • debug_html_to_pic(html, **options) - 渲染 HTML 并返回图片和调试信息
  • text_to_pic(text, css_path, **options) - 将纯文本渲染为图片
  • md_to_pic(md, css_path, **options) - 将 Markdown 渲染为图片
  • template_to_html(template_path, template_name, **kwargs) - 使用 Jinja2 渲染 HTML
  • template_to_pic(template_path, template_name, templates, **options) - 使用 Jinja2 模板渲染图片

配置选项

htmlkit 支持通过 init_fontconfig() 函数配置 fontconfig 选项:

from htmlkit import init_fontconfig

init_fontconfig(
    fontconfig_file="/path/to/fonts.conf",  # 覆盖默认的配置文件
    fontconfig_path="/path/to/fonts",       # 覆盖默认的配置目录
    fontconfig_sysroot="/path/to/sysroot",  # 设置 sysroot
    fc_debug="1",                           # 设置 debug 级别
    fc_lang="zh_CN",                        # 设置默认语言
    fontconfig_use_mmap="yes"               # 是否使用 mmap
)

更多配置选项请参考 fontconfig 文档

构建说明

  1. 安装 Xmake

  2. 初始化环境

    使用 Xmake 时必须激活 Python 虚拟环境,并且安装 build 依赖组。

    # 拉取子模块
    git submodule update --init --recursive
    # 创建虚拟环境并安装依赖,同时避免直接构建项目
    uv sync --no-install-workspace
    # 激活虚拟环境,请使用对应 shell 的命令
    source .venv/bin/activate
    # 配置 Xmake 项目并安装依赖(由于有大量依赖需要通过源码编译安装,可能耗时较长)
    xmake config -m releasedbg
    
  3. 构建并安装

    # 构建
    xmake build
    # 安装
    xmake install
    # 安装到当前虚拟环境
    uv sync --reinstall-package htmlkit
    

    如果对 litehtml 做了修改,则需要重新构建它:

    xmake require --force litehtml
    # 或者用以下更 dirty 但是快速的方法
    rm -r build
    xmake clean --all
    # 重新构建并安装
    xmake build
    xmake install
    uv sync --reinstall-package htmlkit
    

许可证

本项目的 Python 部分 在 MIT 许可证下发布,C++ 部分 在 LGPL-3.0-or-later 许可证下发布。

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyhtmlrender-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl (10.4 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

pyhtmlrender-0.1.1-cp314-cp314t-manylinux_2_28_x86_64.whl (9.3 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.28+ x86-64

pyhtmlrender-0.1.1-cp314-cp314t-macosx_11_0_arm64.whl (6.9 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

pyhtmlrender-0.1.1-cp313-cp313t-win_amd64.whl (7.3 MB view details)

Uploaded CPython 3.13tWindows x86-64

pyhtmlrender-0.1.1-cp313-cp313t-musllinux_1_2_x86_64.whl (10.4 MB view details)

Uploaded CPython 3.13tmusllinux: musl 1.2+ x86-64

pyhtmlrender-0.1.1-cp313-cp313t-musllinux_1_2_aarch64.whl (10.1 MB view details)

Uploaded CPython 3.13tmusllinux: musl 1.2+ ARM64

pyhtmlrender-0.1.1-cp313-cp313t-manylinux_2_28_x86_64.whl (9.3 MB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.28+ x86-64

pyhtmlrender-0.1.1-cp313-cp313t-manylinux_2_28_aarch64.whl (8.9 MB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.28+ ARM64

pyhtmlrender-0.1.1-cp313-cp313t-macosx_11_0_arm64.whl (6.9 MB view details)

Uploaded CPython 3.13tmacOS 11.0+ ARM64

File details

Details for the file pyhtmlrender-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 10.4 MB
  • Tags: CPython 3.14t, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a4cd32b132233d3edf69f0ec6a199938aab6a410610542274006b50ee06d0a97
MD5 4e759e7fdcd49cb9c2e80a890b4a92c6
BLAKE2b-256 c51a70ed9de1a27b2c5d34ccaf0865c3feb4dfb2f2d1ba596eb9b82391212837

See more details on using hashes here.

File details

Details for the file pyhtmlrender-0.1.1-cp314-cp314t-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp314-cp314t-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 9.3 MB
  • Tags: CPython 3.14t, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp314-cp314t-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7a1b5447757e2c93506c5c75f0495c476ec57c17e2162ba8ea5c4e0c2cdf3b69
MD5 4ccf775223a6211dc977ab1b63d2db82
BLAKE2b-256 1c1989edbfbe71e8739180e81dfd12568dd40a2214aaa9c95bd4ca02d1208bf6

See more details on using hashes here.

File details

Details for the file pyhtmlrender-0.1.1-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp314-cp314t-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 6.9 MB
  • Tags: CPython 3.14t, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 412d6dca03217e70efeff178813ada54fcd41ed15737f69cc1e1d6ae089d54cc
MD5 e82c6e66237b415d4d9c4385e12d1ed7
BLAKE2b-256 42b4f0bf750af97ef27c7014f6e79f2015366adec841e20de67f3c1f9ba8e2fe

See more details on using hashes here.

File details

Details for the file pyhtmlrender-0.1.1-cp313-cp313t-win_amd64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp313-cp313t-win_amd64.whl
  • Upload date:
  • Size: 7.3 MB
  • Tags: CPython 3.13t, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp313-cp313t-win_amd64.whl
Algorithm Hash digest
SHA256 741f77047db9b07b1ecfca75edc0409ac44d5047b049710713a882f336943d12
MD5 eb6db032fbc5f7836a399831830db288
BLAKE2b-256 fade3dfc13d2267b714b57df799036f5ad2410fbc685139ed5fc14eb954b1a9d

See more details on using hashes here.

File details

Details for the file pyhtmlrender-0.1.1-cp313-cp313t-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp313-cp313t-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 10.4 MB
  • Tags: CPython 3.13t, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp313-cp313t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ed5c4fb8d8612ea431a23828a1a34ac7a45cee159a0a3370745faebf6b241c73
MD5 69b3d9ab554be7db41f52e41fa39d4e1
BLAKE2b-256 fea8e8b1b89760db709297c6c93825296ba88665ade24b9ed4c026368b2ec876

See more details on using hashes here.

File details

Details for the file pyhtmlrender-0.1.1-cp313-cp313t-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp313-cp313t-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 10.1 MB
  • Tags: CPython 3.13t, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp313-cp313t-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2b98c18c1ad0d2fe66df63d6563effebfcc5b2308dbee58f0711289b3606593d
MD5 83933361be3e7afd08de6dbfc1890eb5
BLAKE2b-256 e035d7bb207db018195354c3267e8369b2aa36bbd1c76886ccb0c9f5bfe8a88e

See more details on using hashes here.

File details

Details for the file pyhtmlrender-0.1.1-cp313-cp313t-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp313-cp313t-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 9.3 MB
  • Tags: CPython 3.13t, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp313-cp313t-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0257c84141d5cff08ba39b3f97a8b0fe7248de7bcb3ae072473ee8c43cc36936
MD5 21285204055a41bc1eeb83ec70a6bb6a
BLAKE2b-256 9ed3e45215989d4786423c1b3fd05c81ca282eebaa3d04a55d1b30a9017449f7

See more details on using hashes here.

File details

Details for the file pyhtmlrender-0.1.1-cp313-cp313t-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp313-cp313t-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 8.9 MB
  • Tags: CPython 3.13t, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp313-cp313t-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 35b02f32cd69f866b193ff91fa0f480911c8ceac7b56d3f917e49198d58a2c8c
MD5 5ce01789cd1e5e0aad92d46aa01e27ad
BLAKE2b-256 1ec76025b2140bbf1dc20c05b4cc5cd6c6765d80036b6f3125f10d47d9527cbb

See more details on using hashes here.

File details

Details for the file pyhtmlrender-0.1.1-cp313-cp313t-macosx_11_0_arm64.whl.

File metadata

  • Download URL: pyhtmlrender-0.1.1-cp313-cp313t-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 6.9 MB
  • Tags: CPython 3.13t, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyhtmlrender-0.1.1-cp313-cp313t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bff4a3e6830190e067a5cd82d7fa104f2596a7ee2f74c32f2e6c6510d5cc8d0b
MD5 224f21cee2d6184e23748ce73e53d91d
BLAKE2b-256 a52038c25da4f3b15132fddf1abcc3dbc68e93d33a6d72eb08d4c1676c4a3681

See more details on using hashes here.

Supported by

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