vue導(dǎo)出少量pdf文件實(shí)現(xiàn)示例詳解
創(chuàng)建js文件 exportPdf.js
且需要安裝html2canvas和jspdf兩個(gè)插件;
然后將在你需要導(dǎo)出的頁面中執(zhí)行方法,
this.getPdf("html", "報(bào)表");
//參數(shù)一:導(dǎo)出范圍的id
//參數(shù)二:導(dǎo)出pdf文件的名稱導(dǎo)出的主要代碼
我在網(wǎng)上查找了大量的代碼才寫出了這個(gè),其他的代碼都是很淺顯,雖然也可以導(dǎo)出,但是一旦數(shù)據(jù)量過大,頁面較長(zhǎng)導(dǎo)出的pdf文件便會(huì)白屏;
此文件解決了此問題,但是因?yàn)閷?dǎo)出的pdf做了分頁,而pdf又是由圖片轉(zhuǎn)換而來,所以分頁中會(huì)有截?cái)嗟膯栴},暫未解決,希望大家可以指點(diǎn)。
/* 導(dǎo)出pdf文檔 */
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
export default {
install(Vue, options) {
Vue.prototype.getPdf = function (id, title) {
const loading = Vue.prototype.$loading({
fullscreen: true,
lock: true,
text: 'Loading',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
let shareContent = document.getElementById(id), //需要截圖的包裹的(原生的)DOM 對(duì)象
width = shareContent.clientWidth, //獲取dom 寬度
height = shareContent.clientHeight, //獲取dom 高度
canvas = document.createElement("canvas"), //創(chuàng)建一個(gè)canvas節(jié)點(diǎn)
scale = 1; //定義任意放大倍數(shù) 支持小數(shù)
canvas.width = width * scale; //定義canvas 寬度 * 縮放
canvas.height = height * scale; //定義canvas高度 *縮放
canvas.style.width = shareContent.clientWidth * scale + "px";
canvas.style.height = shareContent.clientHeight * scale + "px";
canvas.getContext("2d").scale(scale, scale); //獲取context,設(shè)置scale
let opts = {
scale: scale, // 添加的scale 參數(shù)
canvas: canvas, //自定義 canvas
logging: false, //日志開關(guān),便于查看html2canvas的內(nèi)部執(zhí)行流程
width: width, //dom 原始寬度
height: height,
useCORS: true, // 【重要】開啟跨域配置
}
html2Canvas(shareContent, opts).then(() => {
var contentWidth = canvas.width;
var contentHeight = canvas.height;
//一頁pdf顯示html頁面生成的canvas高度;
var pageHeight = (contentWidth / 592.28) * 841.89;
//未生成pdf的html頁面高度
var leftHeight = contentHeight;
//頁面偏移
var position = 0;
//a4紙的尺寸[595.28,841.89],html頁面生成的canvas在pdf中圖片的寬高
var imgWidth = 595.28;
var imgHeight = (592.28 / contentWidth) * contentHeight;
var pageData = canvas.toDataURL("image/jpeg", 1.0);
var 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"); // 這里是導(dǎo)出的文件名
loading.close();
this.$router.go(-1)
});
};
}
};注意:
打印的時(shí)候,父級(jí)或者父級(jí)之上的元素的css設(shè)置不能有transform,否則在火狐瀏覽器中,打印出來的pdf會(huì)有偏移。
以上就是vue導(dǎo)出少量pdf實(shí)現(xiàn)示例詳解的詳細(xì)內(nèi)容,更多關(guān)于vue導(dǎo)出pdf的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Vue-Router 2實(shí)現(xiàn)路由功能實(shí)例詳解
vue-router 2只適用于Vue2.x版本,下面我們是基于vue2.0講的如何使用vue-router 2實(shí)現(xiàn)路由功能,需要的朋友可以參考下2017-11-11
vue+j簡(jiǎn)單的實(shí)現(xiàn)輪播效果,滾動(dòng)公告,銜接
這篇文章主要介紹了vue+j簡(jiǎn)單的實(shí)現(xiàn)輪播效果,滾動(dòng)公告,銜接,文章圍繞主題的相關(guān)資料展開詳細(xì)的內(nèi)容具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
vue2使用el-date-picker實(shí)現(xiàn)動(dòng)態(tài)日期范圍demo
這篇文章主要為大家介紹了vue2使用el-date-picker實(shí)現(xiàn)動(dòng)態(tài)日期范圍示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue動(dòng)態(tài)綁定ref(使用變量)以及獲取方式
這篇文章主要介紹了vue動(dòng)態(tài)綁定ref(使用變量)以及獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue集成kindeditor富文本的實(shí)現(xiàn)示例代碼
這篇文章主要介紹了vue集成kindeditor富文本的實(shí)現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Vue keep-alive實(shí)踐總結(jié)(推薦)
本篇文章主要介紹了Vue keep-alive實(shí)踐總結(jié)(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08

