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>
    
  • 相关阅读:
    tnagios
    python-gearman使用
    yaml
    中国大陆互联网国际出口情况(2015年)
    vsftpd配置
    spoj-ASSIGN-bitDP
    spoj-ANARC05H -dp
    Light oj 1379 -- 最短路
    SPOJ-394-ACODE
    2018年东北农业大学春季校赛
  • 原文地址:https://www.cnblogs.com/FormerWhite/p/15015610.html
Copyright © 2011-2022 走看看