zoukankan      html  css  js  c++  java
  • vue页面导出pdf

    安装插件

    npm install html2canvas --save
    npm install jspdf --save
    

    exportPDF.js

    import html2Canvas from 'html2canvas';
    import JsPDF from 'jspdf';
    
    export default {
     install (Vue, options) {
      Vue.prototype.exportPdfHandler = function (el,title,imgtype) {
        // el 要导出元素的id
        // title 文件名称
        // imgtype 图片类型 默认png
        let imgType = imgtype? imgtype: "png"
        let element = document.getElementById(el)
       html2Canvas(element, {
        allowTaint: true
       }).then(function (canvas) {
        let contentWidth = canvas.width;
        let contentHeight = canvas.height;
        let pageHeight = contentWidth / 592.28 * 841.89;
        let leftHeight = contentHeight;
        let position = 0;
        let imgWidth = 595.28;
        let imgHeight = 592.28 / contentWidth * contentHeight;
        let pageData = canvas.toDataURL('image/' + imgType.toLowerCase(), 1.0);
        let PDF = new JsPDF('', 'pt', 'a4');
        if (leftHeight < pageHeight) {
         PDF.addImage(pageData, imgType.toUpperCase(), 0, 0, imgWidth, imgHeight);
        } else {
         while (leftHeight > 0) {
          PDF.addImage(pageData, imgType.toUpperCase(), 0, position, imgWidth, imgHeight);
          leftHeight -= pageHeight;
          position -= 841.89;
          if (leftHeight > 0) {
           PDF.addPage();
          }
         }
        }
        PDF.save(title + '.pdf');
       })
      }
     }
    }
    
    

    main.js

    import htmlToPdf from './utils/exportPDF.js'
    Vue.use(htmlToPdf)
    

    使用

    <div id="HealthComparison">
      
    </div>
    <el-button size="small" type="primary" @click="exportPdfHandler('需要导出内容的id','文件名称')">导出</el-button>
    
  • 相关阅读:
    区间K 大数查询
    最大最小公倍数
    吃糖果
    身份证号码升级
    威威猫系列之 吃鸡腿
    小Q系列之失恋
    查询7天之内的数据
    WebService案例 Spring boot+CXF开发WebService Demo
    开发过程中遇到问题
    oracle 自增序列 sequence
  • 原文地址:https://www.cnblogs.com/FormerWhite/p/15015610.html
Copyright © 2011-2022 走看看