Here is a detailed code example of using the Vue framework to convert a page to PDF and embed the PDF file into the page using the iframe tag:
<template>
<div>
<button @click="downloadPDF">Download PDF</button>
<iframe :src="pdfUrl" width="100%" height="600px"></iframe>
</div>
</template>
<script>
import html2canvas from 'html2canvas'
import jsPDF from 'jspdf'
export default {
data() {
return {
pdfUrl: ''
}
},
methods: {
async downloadPDF() {
const canvas = await html2canvas(document.body)
const imgData = canvas.toDataURL('image/png')
const pdf = new jsPDF()
pdf.addImage(imgData, 'PNG', 0, 0)
const pdfUrl = pdf.output('datauristring')
this.pdfUrl = pdfUrl
}
}
}
</script>
Memo.