tags: front end javascript vue
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
export default {
install (Vue, options) {
Vue.prototype.getPdf = function (idStr, title) {
html2Canvas(document.querySelector('#' + idStr), {
// allowTaint: true,
useCORS: true
}).then(function (canvas) {
const contentWidth = canvas.width
const contentHeight = canvas.height
// One page PDF shows the CANVAS height generated by the HTML page;
const pageHeight = contentWidth / 592.28 * 841.89
/ / Generate the HTML page height of the PDF
let leftHeight = contentHeight
// Page offset
let position = 0
// A4 paper size [595.28, 841.89], the HTML page generated canvas in PDF in PDF
const imgWidth = 595.28
const imgHeight = 592.28 / contentWidth * contentHeight
// canvas.crossOrigin="anonymous";
const pageData = canvas.toDataURL('image/jpeg', 1.0)
const PDF = new JsPDF('', 'pt', 'a4')
// There are two highly need to distinguish, one is the actual height of the HTML page, and generates PDF page height (841.89)
// When the content does not exceed the scope of the PDF page display, no paging
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) {
/ / Avoid adding a blank page
PDF.addPage()
}
}
}
PDF.save(title + '.pdf')
})
}
}
}
import htmlToPdf from '@/components/htmlToPdf/htmlToPdf'
Vue.use(htmlToPdf)
<template>
<div class="row" id="pdfDom" style="padding-top: 55px;background-color:#fff;">
// PDF wants to display the content
</div>
</template>
<script>
export default {
data () {
return {}
},
method: {
/ / Click to call the method
htmlToPdf () {
this.getPdf('pdfDom', 'Inspection task report')
},
}
}
</script>
1, page introduction JS 2, the ID = "PDF-Wrapper" to be exported, used in the 3rd step FUNCTION according to ID DIV 3, JS download method...
Vue exports the current page is PDF, and then saved to the back end front end Install HTML2CANVAS and JSPDF dependencies Create htmltopdf.js files in Utils, and copy the following code Main.js introdu...
First, VUE exports PDF format Download two modules The code is as follows (example): 2. Define global functions to create an HTMLTOPDF.js file in the specified location. The code is as follows (exampl...
Simple logging VUE exports PDF file 1, download package: npm install html2canvas npm install jspdf 2, write JS import html2Canvas from ‘html2canvas’ import JsPDF from ‘jspdf’ d...
environment vue3.4.0 rely Tools export-pdf.js use .end...
Vue exports PDF documentation Install Import Or in the global main.js global introduction Export PDF 1. Package into a public component 2. Call html...
I. Back code Introduced 2. Business code Tools Service Controller Two. Front end code api.js Notice: responseType:'blob' Must be added, otherwise the export will report the error. VUE component Three ...
refer tohere 1, turn HTML to Picture Canvas Scrolly: refDom.top, // Key code, intercept length Height: refDom.height // Add height, avoid interception The above two attributes are the key to intercept...
4 steps 1, download: npm install --save html2canvas npm install jspdf --save 2, create a new JS file under the Utils folder, and copy the following 3. Introducing the page that needs to be exported in...
HTML exports PDF (available in Vue) 1, plugin installation 2. Register a globally available export method 3. Export PDF in the component 4, IE browser does not currently support downloading, the reaso...