(vue)Vue項(xiàng)目中使用jsPDF和html2canvas生成PDF
效果:
安裝與使用
1.:安裝jsPDF和html2canvas
npm install jspdf html2canvas
2.在需要生成PDF文檔的組件中引入jsPDF和html2canvas
<template>
<div>
<el-button type="primary" @click="exportPDF">導(dǎo)出畫像</el-button>
<div id="pdf-container">
//需要導(dǎo)出pdf的內(nèi)容
</div>
</div>
</template>
<script>
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
data() {
return {}
}
methods: {
// 單頁pdf:css高度自適應(yīng)即可(此處用的一個(gè)css,為了實(shí)現(xiàn)多頁pdf同時(shí)不讓分頁分割圖片,css中寫死了每頁的高度.a4page)
exportPDF() {
var title = "單頁報(bào)告";
var dom = document.getElementById("pdf-container"); // 生成pdf的html內(nèi)容
html2Canvas(dom, {
allowTaint: true,
scrollY: 0,
scrollX: 0,
useCORS: true, // 開啟跨院
// width: 1000, // 寬度
height: dom.offsetHeight,
}).then(function (canvas) {
var contentWidth = canvas.width;
var contentHeight = canvas.height;
var pdfWidth = ((contentWidth + 10) / 2) * 0.75;
var pdfHeight = ((contentHeight + 200) / 2) * 0.75; // 500為底部留白
var imgWidth = pdfWidth;
var imgHeight = (contentHeight / 2) * 0.75; //內(nèi)容圖片這里不需要留白的距離
var pageData = canvas.toDataURL("image/jpeg", 1.0);
var pdf = new JsPDF("", "pt", [pdfWidth, pdfHeight]);
pdf.addImage(pageData, "jpeg", 0, 0, imgWidth, imgHeight);
pdf.save(title + ".pdf");
});
},
}
</script>
解決參考:
1.https://www.jianshu.com/p/31d37bef539b
2.https://www.php.cn/faq/556634.html文章來源:http://www.zghlxwxcb.cn/news/detail-689264.html
3.https://blog.csdn.net/m0_54967474/article/details/123820384文章來源地址http://www.zghlxwxcb.cn/news/detail-689264.html
到了這里,關(guān)于(vue)Vue項(xiàng)目中使用jsPDF和html2canvas生成PDF的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!