[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
我在前端岗位上要实现个可视化图表页的PDF文件导出,在这里给大家分享下使用jsPDF和html2canvas包将Vue页面导出成PDF的方法。
<>1. 下载npm包
npm install html2canvas npm install jspdf
<>2. 创建插件.js文件
Vue-cli项目的话是在./utils文件夹下,我在这里使用的nuxt框架,所以是在./plugins文件夹下。
import html2Canvas from 'html2canvas'; import JsPDF from 'jspdf'; export
default { install (Vue, options) { Vue.prototype.getPdf = function () { var
title= this.pdfTitle; // 导出的pdf文件名 html2Canvas(document.querySelector(this.
pdfSelector), { //导出的html元素 allowTaint: true }).then(function (canvas) { let
contentWidth= canvas.width; let contentHeight = canvas.height; let pageHeight =
contentWidth/ 592.28 * 841.89; let leftHeight = contentHeight; let position = 0;
let imgWidth = 595.28; let imgHeight = 592.28 / contentWidth * contentHeight;
let pageData = canvas.toDataURL('image/jpeg', 1.0); let PDF = new JsPDF('', 'pt'
, 'a4'); if (leftHeight < pageHeight) { 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(title + '.pdf'); })
} } }
上面的插件代码可以直接复制,然后在引用的Vue文件中填入自己的参数就可以了。
<>3. 修改引用页面
导出按钮调用getPdf方法,data填入参数。
<template> <div id="pdfPrint"> <!-- 调用getPdf方法 --> <el-button @click=
"getPdf('#pdfPrint')">保存为PDF</el-button> </div> </template> <script> export
default { data() { return { // 填入导出的pdf文件名和html元素 pdfTitle: '因子评价报告',
pdfSelector: '#pdfPrint', } },
大概就是这样啦,非常地简单,有什么问题可以在下方评论区提问~