banner
leoking

leoking

前端开发者
tg_channel

Vue Page to PDF Display

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.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.