解决QRCode生成二维码打印不出

问题:二维码嵌入在页面中,打印时二维码不渲染的情况

解决方法:

       使用 html2canvas 库将指定的 DOM 元素(在这个例子中是 id 为 pdf 的元素)转换成一个画布(canvas),然后将这个画布转换成图片,创建一个不可见的 iframe 用于打印,将图片插入到 iframe 中并打印出。

解决代码:

import html2Canvas from 'html2canvas';

export const rootPrint = function (ref: any) {
  // alert("建议使用横向打印")
  // const WindowPrt: any = window.open(
  //   "",
  //   "打印",
  //   "width=" +
  //     (screen.availWidth - 10) +
  //     ",height=" +
  //     (screen.availHeight - 50) +
  //     ",left=0,top=0,location=0,menubar=0,toolbar=0,scrollbars=0,status=0" +
  //     '@page{ size: landscape; }'
  // ) // 裸全屏
  // WindowPrt.document.head.innerHTML = window.document.head.innerHTML // 加入当前所有头 - 以防样式丢失
  // WindowPrt.document.body.innerHTML = ref.innerHTML // 载入指定body
  // WindowPrt.print() // 调用打印
  // setTimeout(() => {
  //   WindowPrt.close() // 自动等待print结束后执行
  // }, 1000);
  // window.print()

  alert("建议使用横向打印");

  html2Canvas(document.querySelector('#pdf'), {
    allowTaint: true, //允许画布污染
    taintTest: false, //禁用污染测试
    useCORS: true, //允许加载跨域图片
    dpi: window.devicePixelRatio * 4, // 将分辨率提高到特定的DPI,提高四倍
    scale: 4, // 按比例增加分辨率
  }).then(function (canvas) {
    // 创建一个临时的img元素来展示canvas内容
    var img = new Image();
    img.src = canvas.toDataURL('image/jpeg', 1.0);

    // 调整图片尺寸以适应A4页面
    img.style.width = '794px';
    img.style.height = 'auto'; // 自动调整高度以保持比例
  
    // 创建一个不可见的iframe用于打印
    var iframe = document.createElement('iframe');
    iframe.style.position = 'absolute';
    iframe.style.width = '0px';
    iframe.style.height = '0px';
    iframe.style.border = 'none';
    document.body.appendChild(iframe);
  
    // 将图片插入到iframe中
    iframe.contentWindow.document.open();
    iframe.contentWindow.document.write('<style>@page { size: A4; margin: 0mm; }</style>'); // 设置页面为A4大小,无页边距
    iframe.contentWindow.document.write(img.outerHTML);
    iframe.contentWindow.document.close();
  
    // 延迟执行打印,确保图片完全加载
    setTimeout(() => {
      iframe.contentWindow.focus();
      iframe.contentWindow.print();
      document.body.removeChild(iframe); // 打印后移除iframe
    }, 1000);
  });
}

最终结果: 

要在Vue中使用qrcode生成二维码,你可以按照以下步骤进行操作: 1. 首先,你需要安装qrcode插件,你可以使用npm命令来安装:npm i qrcode -S 。 2. 然后,在你的Vue组件中引入qrcode插件:import QRCode from 'qrcode' 。 3. 接下来,在你的Vue组件的template中添加一个img标签,用于显示生成的二维码图片。例如: ``` <template> <div> <img :src="QRImgUrl" /> </div> </template> ``` 4. 在Vue组件的script中,使用QRCode生成二维码的方法。首先,你需要定义一个data属性QRImgUrl用于存储生成的二维码图片的URL。然后,在created钩子函数中调用getQRcode方法来生成二维码。getQRcode方法使用QRCode.toDataURL来生成二维码图片的DataURL,并将生成的URL赋值给QRImgUrl。例如: ``` <script> import QRCode from 'qrcode' export default { data() { return { QRImgUrl: '', QRlink:'www.xxx.com' } }, created() { this.getQRcode() }, methods: { getQRcode(){ QRCode.toDataURL(this.QRlink, { errorCorrectionLevel: 'L', margin: 2, width: 128 }, (err, url) => { if (err) throw err this.QRImgUrl = url }) } } } </script> ``` 这样,当你的Vue组件被创建时,getQRcode方法会被调用,生成二维码并将URL赋值给QRImgUrl,从而显示在页面上 。 如果你想对生成的二维码进行更详细的配置,你可以参考以下步骤: 1. 在getQRcode方法中定义一个opts对象,用于配置生成二维码的各种参数,比如容错级别、二维码类型、二维码质量、留白边距等 。 2. 修改QRCode.toDataURL方法的第二个参数为opts,这样可以根据opts的配置生成更加符合你需求的二维码 。 例如,你可以按照以下方式配置opts对象: ```javascript let opts = { errorCorrectionLevel: "L",//容错级别 type: "image/png",//生成的二维码类型 quality: 0.3,//二维码质量 margin: 5,//二维码留白边距 width: 128,//宽 height: 128,//高 text: "http://www.xxx.com",//二维码内容 color: { dark: "#666666",//前景色 light: "#fff"//背景色 } }; ``` 然后,将opts作为QRCode.toDataURL方法的第二个参数传入: ```javascript QRCode.toDataURL(this.QRlink, opts, (err, url) => { if (err) throw err this.QRImgUrl = url }) ``` 这样,你就可以根据opts的配置生成定制化的二维码 。 综上所述,你可以按照以上步骤在Vue中使用qrcode生成二维码,并根据需要进行详细的配置 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值