banner
leoking

leoking

前端开发者
tg_channel

vue 页面转 PDF 展示

以下是使用 vue 框架,将页面转换成 PDF 并使用 iframe 标签将 PDF 文件嵌入到页面中的详细代码示例:

<template>
  <div>
    <button @click="downloadPDF">下载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>

记录备忘.

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。