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

前端处理导出PDF。Vue导出pdf

前言:该篇主要是解决一些简单的页面内容导出为PDF

1.安装依赖

使用到两个依赖,项目目录下运行这两个

//页面转换成图片
npm install --save html2canvas 
//图片转换成pdf
npm install jspdf --save 

2.创建通用工具类exportPdf.js文件

可以保存在工具类目录下;

// 导出页面为PDF格式
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'export function getPdf(title, id) {// 参数校验if (!title || !id) {console.error('Title and ID are required.')return}const element = document.querySelector(`#${id}`)if (!element) {console.error(`Element with ID "${id}" not found.`)return}html2Canvas(element, {allowTaint: true,useCORS: true,scale: 2}).then(function (canvas) {const contentWidth = canvas.widthconst contentHeight = canvas.height// A4纸尺寸 (单位: pt)const a4Width = 595.28const a4Height = 841.89// 设置外边距 (单位: pt)const marginLeft = 20const marginTop = 20const marginRight = 20const marginBottom = 20// 计算可用内容区域const availableWidth = a4Width - marginLeft - marginRightconst availableHeight = a4Height - marginTop - marginBottom// 计算缩放比例,使内容宽度适配可用区域const scale = availableWidth / contentWidth// 按比例计算图像在页面上的尺寸const imgWidth = contentWidth * scaleconst imgHeight = contentHeight * scale// 计算每页可容纳的高度(考虑上下边距)const pageHeight = availableHeightlet leftHeight = imgHeightlet position = 0const pageData = canvas.toDataURL('image/jpeg', 0.95)const PDF = new JsPDF('', 'pt', 'a4')if (leftHeight < pageHeight) {// 内容高度小于页面可用高度,直接添加图片(带外边距)PDF.addImage(pageData, 'JPEG', marginLeft, marginTop, imgWidth, imgHeight)} else {// 内容高度大于页面可用高度,分页处理while (leftHeight > 0) {// 添加当前页面图像(带外边距)PDF.addImage(pageData, 'JPEG', marginLeft, marginTop + position, imgWidth, imgHeight)leftHeight -= pageHeightposition -= a4Height// 如果还有剩余内容,添加新页面if (leftHeight > 0) {PDF.addPage()}}}PDF.save(title + '.pdf')}).catch(function (error) {console.error('PDF导出失败:', error)})
}

3.Demo例子

<template><div><!-- 容器 --><div id="pdfHtml" ref="printHtml"><h1>这是一个Demo。</h1></div><button @click="exportPDF">PDF导出</button></div>
</template>
<script>
import { getPdf } from '@/utils/exportPdf';
export default {methods: {// 导出pdfexportPDF() {getPdf('关键因素', 'pdfHtml');}}
}
</script>
http://www.lryc.cn/news/624213.html

相关文章:

  • 备份数据库数据的时候,使用全局锁会影响业务,那有什么其他方式可以避免?
  • Redis---持久化策略
  • 如何用企业微信AI 破解金融服务难题?
  • easyexcel fastexcel 官方文档 easyexcel合并单元格
  • linux:告别SSH断线烦恼,Screen命令核心使用指南
  • 前端上传excel并解析成json
  • 实现自学习系统,输入excel文件,能学习后进行相应回答
  • AI 对话高效输入指令攻略(五):AI+PicDoc文生图表工具:解锁高效图表创作新范式
  • 实战测试:多模态AI在文档解析、图表分析中的准确率对比
  • 2025年8月更新!Windows 7 旗舰版 (32位+64位 轻度优化+离线驱动)
  • 【温室气体数据集】全球总碳柱观测网络 TCCON
  • 基于NLP的文本生成系统设计与实现(LW+源码+讲解+部署)
  • 【硬件-笔试面试题】硬件/电子工程师,笔试面试题-59,(知识点:谐振电路,谐振频率,串联谐振,并联谐振)
  • 【WSL2笔记10】WSL-Ubuntu 环境下 ComfyUI 本地部署性能最大化指南
  • 【Mac】【Minecraft】关于如何在Mac上搭建基岩版MC服务器的方法
  • SIGKDD-2023《Complementary Classifier Induced Partial Label Learning》
  • 如何用github记录mit6s081-2020-labs学习过程
  • 【网络运维】Playbook项目实战:基于 Ansible Playbook 一键部署 LNMP 架构服务器
  • Tmux Xftp及Xshell的服务器使用方法
  • Tomcat Context的核心机制
  • 【GPT入门】第47课 LlamaFacotory 合并原模型与LoRA模型
  • Navicat 无法登录时找回 SQL 文件的方法
  • Zephyr 中的 bt_le_per_adv_set_data 函数的介绍和应用方法
  • RK3568 NPU RKNN(六):RKNPU2 SDK
  • c++之static和const
  • Zephyr 中 BT_GATT_SERVICE_DEFINE 使用详解
  • 面向R语言用户的Highcharts
  • 【1:1复刻R版】python版火山图函数一键出图
  • Zephyr下控制ESP32S3的GPIO口
  • 生信分析自学攻略 | R软件和Rstudio的安装