Vue將頁面導(dǎo)出為圖片或者PDF
本文實例為大家分享了Vue導(dǎo)出頁面為PDF格式的具體代碼,供大家參考,具體內(nèi)容如下
導(dǎo)出為圖片
1.將頁面html轉(zhuǎn)換成圖片
npm install html2canvas --save
2.在需要導(dǎo)出的頁面引入
import html2canvas from 'html2canvas';
在 methods 中添加方法
dataURLToBlob(dataurl) {//ie 圖片轉(zhuǎn)格式 var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], {type: mime}) }, downloadResult(name) { let canvasID = document.body let a = document.createElement('a'); html2canvas(canvasID).then(canvas => { let dom = document.body.appendChild(canvas); dom.style.display = "none"; a.style.display = "none"; document.body.removeChild(dom); let blob = this.dataURLToBlob(dom.toDataURL("image/png")); a.setAttribute("href", URL.createObjectURL(blob)); a.setAttribute("download", name + ".png") document.body.appendChild(a); a.click(); URL.revokeObjectURL(blob); document.body.removeChild(a); }); }, printOut(name) { // 個人觀察只是截取可見范圍以及以下的區(qū)域,所以先將滾動條置頂 $(window).scrollTop(0); // jQuery 的方法 document.body.scrollTop = 0; // IE的 document.documentElement.scrollTop = 0; // 其他 this.downloadResult(name) },
導(dǎo)出為PDF
1.將頁面html轉(zhuǎn)換成圖片
npm install html2canvas --save
2.將圖片生成pdf
npm install jspdf --save
3.在需要導(dǎo)出的頁面引入
import html2canvas from 'html2canvas'; import JsPDF from 'jspdf'
在 methods 中添加方法
printOut(name) { let shareContent = document.body,//需要截圖的包裹的(原生的)DOM 對象 width = shareContent.clientWidth, //獲取dom 寬度 height = shareContent.clientHeight, //獲取dom 高度 canvas = document.createElement("canvas"), //創(chuàng)建一個canvas節(jié)點 scale = 2; //定義任意放大倍數(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(name + ".pdf"); // 這里是導(dǎo)出的文件名 }); },
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用cesium創(chuàng)建數(shù)據(jù)白模方式
這篇文章主要介紹了vue使用cesium創(chuàng)建數(shù)據(jù)白模方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10使用Vue+Django+Ant Design做一個留言評論模塊的示例代碼
這篇文章主要介紹了使用Vue+Django+Ant Design做一個留言評論模塊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06vue3.0+element Plus實現(xiàn)頁面布局側(cè)邊欄菜單路由跳轉(zhuǎn)功能
這篇文章主要介紹了vue3.0+element Plus實現(xiàn)頁面布局,側(cè)邊欄菜單路由跳轉(zhuǎn),需要的朋友可以參考下2023-07-07Vue使用el-input自動獲取焦點和二次獲取焦點問題及解決
這篇文章主要介紹了Vue使用el-input自動獲取焦點和二次獲取焦點問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12