mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2025-07-27 12:10:57 +00:00
feat: 优化了排版和渲染
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
import os.path
|
||||
import time
|
||||
from os import getcwd
|
||||
|
||||
import aiofiles
|
||||
from nonebot import require
|
||||
|
||||
require("nonebot_plugin_htmlrender")
|
||||
@ -31,25 +34,44 @@ from nonebot_plugin_htmlrender import *
|
||||
async def template2image(
|
||||
template: str,
|
||||
templates: dict,
|
||||
pages: dict | None = None,
|
||||
pages=None,
|
||||
wait: int = 0,
|
||||
scale_factor: float = 2,
|
||||
**kwargs
|
||||
scale_factor: float = 1,
|
||||
debug: bool = False,
|
||||
) -> bytes:
|
||||
"""
|
||||
template -> html -> image
|
||||
Args:
|
||||
debug: 输入渲染好的 html
|
||||
wait: 等待时间,单位秒
|
||||
pages: 页面参数
|
||||
template: str: 模板文件
|
||||
templates: dict: 模板参数
|
||||
scale_factor: 缩放因子,越高越清晰
|
||||
**kwargs: page 参数
|
||||
Returns:
|
||||
图片二进制数据
|
||||
"""
|
||||
if pages is None:
|
||||
pages = {
|
||||
"viewport": {
|
||||
"width" : 1080,
|
||||
"height": 10
|
||||
},
|
||||
"base_url": f"file://{getcwd()}",
|
||||
}
|
||||
template_path = os.path.dirname(template)
|
||||
template_name = os.path.basename(template)
|
||||
|
||||
if debug:
|
||||
raw_html = await template_to_html(
|
||||
template_name=template_name,
|
||||
template_path=template_path,
|
||||
**templates,
|
||||
)
|
||||
async with aiofiles.open(os.path.join(template_path, "latest-debug.html"), "w", encoding="utf-8") as f:
|
||||
await f.write(raw_html)
|
||||
nonebot.logger.info("Debug HTML: %s" % "latest-debug.html")
|
||||
|
||||
return await template_to_pic(
|
||||
template_name=template_name,
|
||||
template_path=template_path,
|
||||
|
Reference in New Issue
Block a user