赞
踩
htmlToPdf.js
// 导出页面为PDF格式 import html2Canvas from 'html2canvas' import JsPDF from 'jspdf' export default { install(Vue, options) { Vue.prototype.getPdf = function (id) { var title = '导出试卷' if (id === null || id === undefined) { id = '#pdfDom' } html2Canvas(document.querySelector(id), { allowTaint: true, }).then(function (canvas) { const contentWidth = canvas.width const contentHeight = canvas.height const pageData = canvas.toDataURL('image/jpeg', 1.0) const pdfX = contentWidth const pdfY = contentHeight // 500为底部留白 const imgX = contentWidth const imgY = contentHeight // 内容图片这里不需要留白的距离 const PDF = new JsPDF('', 'pt', [pdfX, pdfY]) PDF.addImage(pageData, 'jpeg', 0, 0, imgX, imgY) PDF.save(title + '.pdf') }) } Vue.prototype.getPdfByIds = function (pages) { var title = '导出试卷' let params = [] let PDF = null let imgs = document.querySelectorAll('img') for (let img of imgs) { img.setAttribute('crossorigin', '*') } let parentNode = document.querySelector(pages) let promises = [] for (let i = 0; i < parentNode.childNodes.length; i++) { let promise = new Promise((res, rej) => { let pageIndex = i html2Canvas( document.querySelector('#' + parentNode.childNodes[i].id), { allowTaint: true, // useCORS: true, } ) .then(function (canvas) { const contentWidth = canvas.width const contentHeight = canvas.height let pageData = canvas.toDataURL('image/jpeg', 1.0) let pdfWidth = contentWidth let pdfHeight = contentHeight // 500为底部留白 let imgX = contentWidth let imgY = contentHeight // 内容图片这里不需要留白的距离 params.push({ pageIndex: pageIndex, pageData: pageData, pdfWidth: pdfWidth, pdfHeight: pdfHeight, }) res(PDF) }) .catch((e) => { console.log('报错', e) }) }) promises.push(promise) } Promise.all(promises).then((val) => { params.sort(function (a, b) { let index1 = a['pageIndex'] let index2 = b['pageIndex'] return index1 - index2 //正序 }) let i = 0 for (let param of params) { i++ console.log('param', param) let pdfWidth = param['pdfWidth'] let pdfHeight = param['pdfHeight'] let pageData = param['pageData'] if (PDF === null) { PDF = new JsPDF('', 'pt', [pdfWidth, pdfHeight]) } else { PDF.addPage() } PDF.addImage(pageData, 'jpeg', 0, 0, pdfWidth, pdfHeight) } if (params.length == parentNode.childNodes.length) { PDF.save(title + '.pdf') } }) } Vue.prototype.getPdfByIdsB = function (pages) { var title = '导出试卷' let params = [] let PDF = null let imgs = document.querySelectorAll('img') for (let img of imgs) { img.setAttribute('crossorigin', '*') } let parentNode = document.querySelector(pages) let promises = [] for (let i = 0; i < parentNode.childNodes.length; i++) { let promise = new Promise((res, rej) => { let pageIndex = i html2Canvas( document.querySelector('#' + parentNode.childNodes[i].id), { allowTaint: true, useCORS: true, } ) .then(function (canvas) { const contentWidth = canvas.width const contentHeight = canvas.height let pageData = canvas.toDataURL('image/jpeg', 1.0) let pdfWidth = contentWidth let pdfHeight = contentHeight // 500为底部留白 let imgX = contentWidth let imgY = contentHeight // 内容图片这里不需要留白的距离 params.push({ pageIndex: pageIndex, pageData: pageData, pdfWidth: pdfWidth, pdfHeight: pdfHeight, }) res(PDF) }) .catch((e) => { console.log('报错', e) }) }) promises.push(promise) } Promise.all(promises).then((val) => { params.sort(function (a, b) { let index1 = a['pageIndex'] let index2 = b['pageIndex'] return index1 - index2 //正序 }) let i = 0 for (let param of params) { i++ console.log('param', param) let pdfWidth = param['pdfWidth'] let pdfHeight = param['pdfHeight'] let pageData = param['pageData'] if (PDF === null) { PDF = new JsPDF('', 'pt', [pdfWidth, pdfHeight]) } else { PDF.addPage() } PDF.addImage(pageData, 'jpeg', 0, 0, pdfWidth, pdfHeight) } if (params.length == parentNode.childNodes.length) { PDF.save(title + '.pdf') } }) } }, }
import htmlToPdf from './utils/htmlToPdf'
Vue.use(htmlToPdf)
//下载
downloadPapaers() {
Vue.prototype.getPdfByIdsB('#app')
},
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。