js 导出pdf
首先需要两个插件:
html2canvas:http://html2canvas.hertzen.com/documentation
jsPDF:http://www.rotisedapsales.com/snr/cloud2/website/jsPDF-master/docs/jsPDF.html
导出代码:DOM是要导出的html节点
html2canvas(DOM).then(canvas => { // document.body.appendChild(canvas); const imageData = canvas.toDataURL("image/jpeg", 1); // eslint-disable-next-line new-cap const doc = new jsPDF({ userUnit: 2, unit: "px", format: [canvas.width, canvas.height] }); doc.addImage(imageData, "jpeg", 0, 0, canvas.width, canvas.height); doc.save("应对报告.pdf"); });
a4纸分页:
// const domList = document.querySelectorAll('.pdf-report .export-pdf-class') // console.log(domList) // eslint-disable-next-line new-cap // const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }) // domList.forEach(q => { // console.log(q) // html2canvas(q).then(canvas => { // const imageData = canvas.toDataURL('image/jpeg', 1) // doc.addImage(imageData, 'jpeg', 0, 0, canvas.width, canvas.height) // }) // }) // doc.save(`${row.surveyObject}.pdf`) // const DOM = this.$refs.webReport // html2canvas(DOM).then(canvas => { // const contentWidth = canvas.width // const contentHeight = canvas.height // const pageHeight = contentWidth / 595.23 * 841.89 // let leftHeight = contentHeight // let position = 0 // const imgWidth = 595.23 // const imgHeight = 595.23 / contentWidth * contentHeight // // document.body.appendChild(canvas); // const imageData = canvas.toDataURL('image/jpeg', 1) // // eslint-disable-next-line new-cap // const doc = new jsPDF({ unit: 'pt', format: 'a4' }) // if (leftHeight < pageHeight) { // doc.addImage(imageData, 'jpeg', 0, 0, imgWidth, imgHeight) // } else { // while (leftHeight > 0) { // doc.addImage(imageData, 'jpeg', 0, position, imgWidth, imgHeight) // leftHeight -= pageHeight // position -= 841.89 // if (leftHeight > 0) { // doc.addPage() // } // } // } // // doc.addImage(imageData, 'jpeg', 0, 0, canvas.width, canvas.height) // doc.save(`${row.surveyObject}.pdf`) // })
a0: [1189, 841], a1: [841, 594], a2: [594, 420], a3: [420, 297], a4: [297, 210], // [841.83, 595.23] unit: 'pt' a5: [210, 148] 1pt=0.3528mm。
1pt=0.35146mm。
记录学习每一天。