如何更换网站服务器,网站生成静态,网站推广软件免费版大全,微信服务号可以做万网站么将Vue单页面转成pdf并下载
步骤1#xff1a;下载对应的库
npm install html2canvas;npm install jspdf --save
步骤2#xff1a;创建一个htmlToPdf.js的js文件, 然后在main.js中全局引用一下#xff0c;编写如下代码#xff1a;
// htmlToPdf.js
// 导出页面为PDF格式
…将Vue单页面转成pdf并下载
步骤1下载对应的库
npm install html2canvas;npm install jspdf --save
步骤2创建一个htmlToPdf.js的js文件, 然后在main.js中全局引用一下编写如下代码
// htmlToPdf.js
// 导出页面为PDF格式
import html2Canvas from html2canvas
import JsPDF from jspdf
export default {install(Vue, options) {Vue.prototype.getPdf function () {var title this.htmlTitle //DPF标题html2Canvas(document.querySelector(#pdfDom), {allowTaint: true,taintTest: false,useCORS: true,y:72, // 对Y轴进行裁切// width:1200,// height:5000,dpi: window.devicePixelRatio * 4, //将分辨率提高到特定的DPI 提高四倍scale: 4 //按比例增加分辨率 }).then(function (canvas) {let contentWidth canvas.widthlet contentHeight canvas.heightlet pageHeight contentWidth / 592.28 * 841.89let leftHeight contentHeightlet position 0let imgWidth 595.28let imgHeight 592.28 / contentWidth * contentHeightlet pageData canvas.toDataURL(image/jpeg, 1.0)let PDF new JsPDF(, pt, a4)if (leftHeight pageHeight) {PDF.addImage(pageData, JPEG, 0, 0, imgWidth, imgHeight)} else {while (leftHeight 0) {PDF.addImage(pageData, JPEG, 0, position, imgWidth, imgHeight)leftHeight - pageHeightposition - 841.89if (leftHeight 0) {PDF.addPage()}}}PDF.save(title .pdf)})}}
}
// main.js
import htmlToPdf from /util/htmlToPdf
Vue.use(htmlToPdf)
步骤三在vue页面中给需要打印的区域一个div标签然后给div一个idid名和htmlToPdf.js中选择的名字一致代码如下
div idpdfDom... 将要打印的内容放入其中
/div
步骤四在data节点中声明一个htmlTitle变量指定为pdf文件的文件名
data() {return {htmlTitle: 要生成pdf的文件名}
}
步骤五在vue页面中添加一个button按钮事件名和htmlToPdf.js中的方法名保持一致。
el-button clickgetPdf()PDF/el-button
欧克了完成以上步骤就可以实现div中内容的转为pdf并下载文件 文章转载自: http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.lcmhq.cn.gov.cn.lcmhq.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.lmctj.cn.gov.cn.lmctj.cn http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.xmnlc.cn.gov.cn.xmnlc.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.trrhj.cn.gov.cn.trrhj.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.wqfj.cn.gov.cn.wqfj.cn http://www.morning.jjzbx.cn.gov.cn.jjzbx.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.sbrpz.cn.gov.cn.sbrpz.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.sjwiki.com.gov.cn.sjwiki.com http://www.morning.gtcym.cn.gov.cn.gtcym.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.kybpj.cn.gov.cn.kybpj.cn http://www.morning.wklrz.cn.gov.cn.wklrz.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn