1- import logging
2-
31from .util import generate_data_path
42from playwright .async_api import async_playwright
53from jinja2 .sandbox import SandboxedEnvironment
64from pydantic import BaseModel
75from typing_extensions import TypedDict
86from typing import Literal
7+ from loguru import logger
8+
99
1010class FloatRect (TypedDict ):
1111 x : float
1212 y : float
1313 width : float
1414 height : float
1515
16+
1617class ScreenshotOptions (BaseModel ):
1718 """Playwright 截图参数
1819
@@ -36,15 +37,15 @@ class ScreenshotOptions(BaseModel):
3637 @author: Redlnn(https://github.com/GraiaCommunity/graiax-text2img-playwright)
3738 """
3839
39- timeout : float | None
40- type : Literal ["jpeg" , "png" , None ]
41- quality : int | None
42- omit_background : bool | None
43- full_page : bool | None
44- clip : FloatRect | None
45- animations : Literal ["allow" , "disabled" , None ]
46- caret : Literal ["hide" , "initial" , None ]
47- scale : Literal ["css" , "device" , None ]
40+ timeout : float | None = None
41+ type : Literal ["jpeg" , "png" , None ] = None
42+ quality : int | None = None
43+ omit_background : bool | None = None
44+ full_page : bool | None = True
45+ clip : FloatRect | None = None
46+ animations : Literal ["allow" , "disabled" , None ] = None
47+ caret : Literal ["hide" , "initial" , None ] = None
48+ scale : Literal ["css" , "device" , None ] = None
4849
4950
5051class Text2ImgRender :
@@ -72,16 +73,17 @@ async def html2pic(
7273 if self .context is None :
7374 self .playwright = await async_playwright ().start ()
7475 self .browser = await self .playwright .chromium .launch ()
75- self .context = await self .browser .new_context ()
76- result_path , _ = generate_data_path (suffix = "jpeg" , namespace = "rendered" )
77-
78- logging .info (f"Rendering { html_file_path } " )
76+ self .context = await self .browser .new_context (
77+ device_scale_factor = 1.8 ,
78+ )
7979
80+ suffix = screenshot_options .type if screenshot_options .type else "png"
81+ result_path , _ = generate_data_path (suffix = suffix , namespace = "rendered" )
8082 page = await self .context .new_page ()
8183 await page .goto (f"file://{ html_file_path } " )
8284 await page .screenshot (path = result_path , ** screenshot_options .model_dump ())
8385 await page .close ()
8486
85- logging .info (f"Rendered { html_file_path } to { result_path } " )
87+ logger .info (f"Rendered { html_file_path } to { result_path } " )
8688
8789 return result_path
0 commit comments