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;`
        });

效果图:

实际打印:

相关推荐
于慨15 分钟前
Capacitor
前端
该怎么办呢17 分钟前
Source/Core/Event.js
开发语言·javascript·ecmascript·cesium
IT凝冬37 分钟前
liunx 的 centos7 安装ngin
前端
赵锦川38 分钟前
大屏比例缩放
前端·javascript·html
该怎么办呢1 小时前
Source/Core/DeveloperError.js
开发语言·javascript·ecmascript
于慨1 小时前
tauri
java·服务器·前端
贼爱学习的小黄2 小时前
NC BIP参照开发
java·前端·nc
weixin_462901972 小时前
ESP32 LED控制代码解析
javascript
小江的记录本2 小时前
【MyBatis-Plus】MyBatis-Plus的核心特性、条件构造器、分页插件、乐观锁插件
java·前端·spring boot·后端·sql·tomcat·mybatis
小张会进步2 小时前
数组:二维数组
java·javascript·算法