html2canvas+pdfjs 打印html

项目中需要将页面中的某个功能块进行打印。

思路:使用html2canvas将html转化成图片,打印这个图片

遇到的问题: 打印图片的时候,出现模糊的问题,进行scale放大,导致打印的整体变大

解决办法:

出现模糊的问题: 设置scale值,

导致打印的整体变大:对imageStyle进行设置

javascript 复制代码
     const printArea = document.getElementById("print-area");
        const canvas = await html2canvas(printArea, {
          useCORS: true,
          allowTaint: true,
          scale: 8,
        });
        const imgSrc = canvas.toDataURL("image/png");
        printJS({
          printable: imgSrc,
          type: "image",
          documentTitle: "--",
          base64: "true",
          imageStyle: `width:${printArea.offsetWidth}px;height:${printArea.offsetHeight}px;`
        });

效果图:

实际打印:

相关推荐
文心快码BaiduComate1 小时前
全运会,用文心快码做个微信小程序帮我找「观赛搭子」
前端·人工智能·微信小程序
合作小小程序员小小店1 小时前
web网页开发,在线%档案管理%系统,基于Idea,html,css,jQuery,java,ssh,mysql。
java·前端·mysql·jdk·html·ssh·intellij-idea
合作小小程序员小小店1 小时前
web网页开发,在线%物流配送管理%系统,基于Idea,html,css,jQuery,java,ssh,mysql。
java·前端·css·数据库·jdk·html·intellij-idea
放飞自我的Coder1 小时前
【ip 扫描html小工具】
tcp/ip·html
三门1 小时前
web接入扣子私有化智能体
前端
林小帅1 小时前
AI “自动驾驶” 的使用分享
前端
起名时在学Aiifox1 小时前
深入解析 Electron 打包中的 EPERM: operation not permitted 错误
前端·javascript·electron
游戏开发爱好者81 小时前
Fiddler抓包工具完整教程 HTTPHTTPS抓包、代理配置与API调试实战技巧(开发者进阶指南)
前端·测试工具·ios·小程序·fiddler·uni-app·webview
hachi03132 小时前
Vue中input disabled时点击事件不触发怎么办?
javascript·vue.js·ecmascript