소스 검색

模版自定义模版高度自适配

枫林 3 달 전
부모
커밋
e9ec5ceed7
2개의 변경된 파일10개의 추가작업 그리고 4개의 파일을 삭제
  1. 9 3
      src/lib/Puppeteer.ts
  2. 1 1
      src/plugins/test.ts

+ 9 - 3
src/lib/Puppeteer.ts

@@ -29,13 +29,12 @@ export class HtmlImg {
     }) {
         try {
             await this.init();
-
             const {
                 template,
                 templateIsPath = true,
                 data,
                 width = 800,
-                height = 600,
+                height = 1,
                 type = 'png',
                 quality = 100,
                 fullPage = false,
@@ -48,12 +47,19 @@ export class HtmlImg {
             // 渲染HTML
 
             const html = art.render(templateContent, data);
+            // 计算高度
 
             // 创建页面
             const page = await this.browser!.newPage();
             await page.setViewport({ width, height });
             await page.setContent(html, { waitUntil: 'networkidle0' });
-
+            // 获取document.body.scrollHeight
+            
+            const bodyheight = await page.evaluate(() => document.body.scrollHeight)  as number;
+            botlogger.info(`获取body高度${bodyheight}`)
+            if(bodyheight != height){
+                await page.setViewport({ width, height: bodyheight });
+            }
             // 截图
             const image = await page.screenshot({
                 type: type as 'png' | 'jpeg',

+ 1 - 1
src/plugins/test.ts

@@ -58,7 +58,7 @@ export class test {
                 path: path.resolve(__dirname, '..', 'resources', 'test', 'param.html'),//模版路径,推荐按规范放置在resources目录下
                 render: {//浏览器默认参数设置,用于打开浏览器的设置
                     width: 600, // 模板宽度
-                    height: 300,// 模板高度
+                    height: 1, // 模板高度
                     type: 'png',// 模板类型
                     quality: 100,// 模板质量
                     fullPage: false,// 是否全屏