vue3实现导出pdf、png功能

准备做的系统中出现了 想导出当前页面的png或者pdf设计数据较多后端做可能比较麻烦 就自己研究了一下

1、安装html2canvas 、jspdf包

npm install --save html2canvas   // 可以将dom元素转为一张图片
npm install --save jspdf   // 导出为PDF格式

2、vue组件中引用,代码如下:

<template>
 <div class="content">
    <a-button @click="exportPNG" size="small" type="primary">导出PNG</a-button>
	<a-button @click="exportPDF" size="small" type="primary">导出PDF</a-button>
    <div id="main-charts">
        需要截取的内容区域
        我想测试导出是否可行
    </div>
 </div>
</template>

3、导出png

<script lang="ts" setup>
  // 引入插件
  import html2canvas from 'html2canvas';
  import jsPDF from 'jspdf';
 
  // 导出png
  const exportPNG = () => {
	const ele: HTMLElement | null = document.getElementById('main-charts');
	html2canvas(ele as HTMLElement).then((canvas: any) => {
	  const contentWidth = canvas.width;
	  const contentHeight = canvas.height;
	  const ctx: any = canvas.getContext('2d');
      // 添加水印
	  ctx.textAlign = 'center';
	  ctx.textBaseline = 'middle';
      ctx.rotate((25 * Math.PI) / 180);
	  ctx.font = '20px Microsoft Yahei';
	  ctx.fillStyle = 'rgba(184, 184, 184, 0.8)';
	  for (let i = contentWidth * -1; i < contentWidth; i += 240) {
        for (let j = contentHeight * -1; j < contentHeight; j += 100) {
          // 填充文字,x 间距, y 间距
          ctx.fillText('水印名', i, j);
        }
      }
	  const imgUrl = canvas.toDataURL('image/png');
	  const tempLink = document.createElement('a'); // 创建一个a标签
	  tempLink.style.display = 'none';
	  tempLink.href = imgUrl;
	  tempLink.setAttribute('download', '文件名'); // 给a标签添加下载属性
	  if (typeof tempLink.download === 'undefined') {
		tempLink.setAttribute('target', '_blank');
	  }
	  document.body.appendChild(tempLink); // 将a标签添加到body当中
	  tempLink.click(); // 启动下载
	  document.body.removeChild(tempLink); // 下载完毕删除a标签
	  window.URL.revokeObjectURL(imgUrl);
	})
  }
</script>

4、导出pdf

<script lang="ts" setup>
  // 引入插件
  import html2canvas from 'html2canvas';
  import jsPDF from 'jspdf';
    
  const exportPDF = () => {
	const ele: HTMLElement | null = document.getElementById('main-charts');
	html2canvas(ele as HTMLElement, {
	  dpi: 96, // 分辨率
	  scale: 2, // 设置缩放
	  useCORS: true, // 允许canvas画布内 可以跨域请求外部链接图片, 允许跨域请求。,
	  // backgroundColor:'#ffffff',这样背景还是黑的
	  bgcolor: '#ffffff', // 应该这样写
	  logging: false, // 打印日志用的 可以不加默认为false
	}).then((canvas) => {
	  const contentWidth = canvas.width;
	  const contentHeight = canvas.height;
	  // 一页pdf显示html页面生成的canvas高度;
	  const pageHeight = (contentWidth / 592.28) * 841.89;
	  // 未生成pdf的html页面高度
	  let leftHeight = contentHeight;
	  // 页面偏移
	  let position = 0;
	  // a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
	  const imgWidth = 595.28;
	  const imgHeight = (595.28 / contentWidth) * contentHeight;
	  const ctx: any = canvas.getContext('2d');
      // 添加水印
      ctx.textAlign = 'center';
	  ctx.textBaseline = 'middle';
      ctx.rotate((25 * Math.PI) / 180);
	  ctx.font = '20px Microsoft Yahei';
	  ctx.fillStyle = 'rgba(184, 184, 184, 0.8)';
      for (let i = contentWidth * -1; i < contentWidth; i += 240) {
        for (let j = contentHeight * -1; j < contentHeight; j += 100) {
          // 填充文字,x 间距, y 间距
          ctx.fillText('水印名', i, j);
        }
      }
	  const pageData = canvas.toDataURL('image/jpeg', 1.0);
	  const pdf = new jsPDF('', 'pt', 'a4');
	  if (leftHeight < pageHeight) {
		// 在pdf.addImage(pageData, 'JPEG', 左,上,宽度,高度)设置在pdf中显示;
		pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
	  } else {
		// 分页
		while (leftHeight > 0) {
		  pdf.addImage(
			pageData,
			'JPEG',
			0,
			position,
			imgWidth,
			imgHeight
		  );
		  leftHeight -= pageHeight;
		  position -= 841.89;
		  // 避免添加空白页
		  if (leftHeight > 0) {
			pdf.addPage();
		  }
		}
	  }
	  // 可动态生成
	  pdf.save(`文件名.pdf`);
	});
  }
</script>

相关推荐

  1. vue3实现导出pdf、png功能

    2024-04-07 09:48:02       22 阅读
  2. fastapi+vue实现导出功能

    2024-04-07 09:48:02       10 阅读
  3. Spring Boot + Vue 实现文件导入导出功能

    2024-04-07 09:48:02       21 阅读
  4. vue3导入文件夹、导入文件、导出zip、导出

    2024-04-07 09:48:02       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-07 09:48:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-07 09:48:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-07 09:48:02       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-07 09:48:02       20 阅读

热门阅读

  1. Vue3:优化-从响应式数据中获取纯数据

    2024-04-07 09:48:02       18 阅读
  2. 用GPT-4调试 用Claude 3编码

    2024-04-07 09:48:02       16 阅读
  3. 前端进阶特训营-班会

    2024-04-07 09:48:02       18 阅读
  4. 内网安全之域内用户名枚举

    2024-04-07 09:48:02       47 阅读
  5. 计算机视觉入目要学习哪些东西及就业方向

    2024-04-07 09:48:02       25 阅读
  6. 1.接口自动化测试学习

    2024-04-07 09:48:02       15 阅读
  7. CSS中display: inline-block;的使用

    2024-04-07 09:48:02       18 阅读
  8. 前端开发语言概览:现代技术的演变与应用

    2024-04-07 09:48:02       22 阅读
  9. Matlab中的参数定义

    2024-04-07 09:48:02       19 阅读
  10. Mybatis

    Mybatis

    2024-04-07 09:48:02      17 阅读
  11. 简易通讯录管理系统:C语言实现及代码详解

    2024-04-07 09:48:02       14 阅读
  12. http请求处理相关注解、cookiesession

    2024-04-07 09:48:02       17 阅读