赞
踩
npm install --save html2canvas // 页面转图片
npm install jspdf --save // 图片转pdf
<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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。