页面DOM导出PDF
Nginx设置允许跨域
1 2 3 4 5 6
| location /static { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; index index.html; root /data/wwwjarapi/8905xhkjfileapitest/; }
|
也就是在location下添加
1 2
| add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true';
|
添加模块引用
第一个将页面html转换成图片
1
| npm install html2canvas --save
|
第二个将图片生成pdf
1
| npm install jspdf --save
|
未使用VUE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import html2canvas from 'html2canvas'; import JsPDF from 'jspdf';
function downloadPDF(ele, pdfName){ document.documentElement.scrollTop = 0; html2canvas( ele, { dpi: 300, useCORS:true } ).then( (canvas)=>{ var contentWidth = canvas.width; var contentHeight = canvas.height; var pageHeight = contentWidth / 592.28 * 841.89; var leftHeight = contentHeight; var position = 0; var imgWidth = 595.28; var imgHeight = 595.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(pdfName); }) } export default { downloadPDF }
|
注意
document.documentElement.scrollTop = 0; 是为了解决外层页面滚动时获取的canvas偏移的问题
引用
1
| import htmlToPdf from "@/assets/js/htmlToPdf";
|
调用
1 2 3 4
| htmlToPdf.downloadPDF( document.querySelector("#myexport"), "小明的试卷" );
|
使用VUE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import html2Canvas from 'html2canvas' import JsPDF from 'jspdf'
export default{ install (Vue, options) { Vue.prototype.getPdf = function (id,title) { document.documentElement.scrollTop = 0; html2Canvas(document.querySelector(`#${id}`), { useCORS: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') } ) } } }
|
main.js文件中添加如下代码:
1 2
| import htmlToPdf from '@/utils/htmlToPdf' Vue.use(htmlToPdf)
|
然后就可以在要导出pdf文件组件里面添加 如下 代码即可导出
1
| this.getPdf('resumeId',name)
|
Canvas转图片下载
也可以使用html2canvas获取canvas后 转为图片下载
1 2 3 4 5 6 7
| let url = canvas.toDataURL("image/png"); var oA = document.createElement("a"); oA.download = ''; oA.href = url; document.body.appendChild(oA); oA.click(); oA.remove();
|