当前位置:   article > 正文

vue导出pdf_vue转pdf

vue转pdf
npm install --save html2canvas  // 页面转图片
npm install jspdf --save  // 图片转pdf

  • 1
  • 2
  • 3
<template>
  <div id="pdf"">
    这是待转换的页面,点击 
   <el-button class="whitebtn" @click="pdf">
      导出pdf
    </el-button>
  </div>
</template>

<script>
import html2canvas from "html2canvas"
import jsPDF from "jspdf"
export default {
  data() {
    return {};
  },
  methods: {
   this.downloadPDF(document.querySelector('#pdf'))
  },
  downloadPDF(page) {
            html2canvas(page).then(function(canvas) {
                console.log(canvas)
                let imgHeight = canvas.height
                let imgWidth = canvas.width
                // 第一个参数: l:横向  p:纵向
                // 第二个参数:测量单位("pt","mm", "cm", "m", "in" or "px")
                let pdf = new jsPDF("l", "pt")

                pdf.addImage(
                    canvas.toDataURL("image/jpeg", 1.0),
                    "JPEG",
                    0,
                    0,
                    imgWidth,
                    imgHeight
                )
                pdf.save("导出.pdf")
            })
        },
};
</script>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/880338
推荐阅读