当前位置: 首页 > news >正文

Vue页面生成PDF后调起浏览器打印

一、安装依赖

首先,需要安装 html2canvas 和 jsPDF 库。

npm install html2canvas jspdf

二、创建公共方法+引入

在utils文件夹下创建两个文件分别为pdfExport.js和printPDF.js,代码如下:

  • pdfExport.js
import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';export const exportToPDF = async (elementId) => {console.log('Exporting PDF...');const content = document.getElementById(elementId);if (!content) {console.error(`Element with id ${elementId} not found.`);return;}try {const canvas = await html2canvas(content);const imgData = canvas.toDataURL('image/png');const pdf = new jsPDF({orientation: 'portrait',unit: 'mm',format: 'a4'});// 获取页面尺寸const pageWidth = pdf.internal.pageSize.getWidth();const pageHeight = pdf.internal.pageSize.getHeight();// 计算图片宽高比const imgWidth = pageWidth;const imgHeight = (canvas.height * imgWidth) / canvas.width;// 分页添加图片let position = 0;while (position < imgHeight) {pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);position += pageHeight;if (position < imgHeight) {pdf.addPage();}}// 将 PDF 转换为 Blob 对象const blob = pdf.output('blob');console.log('PDF Blob generated:', blob); // 打印生成的 Blob 对象信息return blob; // 返回生成的 Blob 对象,供后续使用} catch (error) {console.error('导出 PDF 失败:', error);throw error; // 抛出异常供调用者处理}
};
  • printPDF.js
export const printPDF = (blob) => {try {const url = URL.createObjectURL(blob);console.log('PDF Blob URL:', url); // 打印生成的 Blob URL// 创建一个隐藏的 iframeconst iframe = document.createElement('iframe');iframe.style.position = 'fixed';iframe.style.right = '0';iframe.style.bottom = '0';iframe.style.width = '0';iframe.style.height = '0';iframe.style.border = 'none';document.body.appendChild(iframe);// 设置 iframe 的 src 属性为 PDF 文件的 URLiframe.src = url;// 加载完成在进行打印操作,确保 PDF 加载完成iframe.onload = function() {iframe.contentWindow.focus();iframe.contentWindow.print();// 打印完成后移除 iframe 和释放 URLsetTimeout(() => {// document.body.removeChild(iframe);URL.revokeObjectURL(url);}, 500)};} catch (error) {console.error('打印 PDF 出错:', error);throw error;}
};

创建好后在main.js中引入方法

import { exportToPDF } from '../utils/pdfExport';
Vue.prototype.$exportToPDF = exportToPDF;import { printPDF } from '../utils/printPDF';
Vue.prototype.$printPDF = printPDF;

三、使用

  • html
<template><div><!-- 打印按钮 --><span class="mr20" @click="printPageFn">打印页面</span><!-- 需要打印的页面内容,可以是任意div,设置id就可以 --><router-view id="contentToExport"/></div>
</template>
  • Javascript
// 打印页面
printPageFn(){//注意传入的id是否与页面设置的id一致this.$exportToPDF('contentToExport').then((blob) => {this.$printPDF(blob);});
},
http://www.lryc.cn/news/378282.html

相关文章:

  • 纯前端实现导出excel
  • QT windows 5.12.0 安装包
  • 改进YOLOv7 | 在 ELAN 模块中添加【Triplet】【SpatialGroupEnhance】【NAM】【S2】注意力机制 | 附详细结构图
  • windows系统停止更新办法
  • 数据标注概念
  • 网络安全复习笔记
  • Laravel - excel 导入数据
  • 移动语义和完美转发
  • 【IDEA】Spring项目build失败
  • 【无标题】安卓app 流量
  • 国产化ETL产品必备的特性(非开源包装)
  • flink 操作mongodb的例子
  • 【笔记】打卡01 | 初学入门
  • Rocky9使用cockpitweb登陆时root用户无法登陆
  • 微信小程序修改标题
  • Linux MySQL服务设置开机自启动
  • MacOS设备远程登录配置结合内网穿透实现异地ssh远程连接
  • 国有企业如何提高人效比?
  • Leetcode - 周赛401
  • Java | Leetcode Java题解之第171题Excel表列序号
  • 【uni-app学习手札】
  • ASP.NET Core 中使用 Dapper 的 Oracle 存储过程输出参数
  • C++的动态内存分配
  • 【论文阅读】-- TSR-TVD:时变数据分析和可视化的时间超分辨率
  • 《web应用技术》第12次课后作业
  • 【初阶数据结构】深入解析带头双向循环链表:探索底层逻辑
  • 【面试干货】Java中的访问修饰符与访问级别
  • Oracle最终还是杀死了MySQL
  • 【Python的随机数汇总】
  • [状态压缩 广搜BFS]Saving Tang Monk