Skip to content

html转pdf下载(htmlPdf) ​

说明 ​

html转pdf下载(htmlPdf),V3JS框架中常用的基础库。

1.初始化websocket ​

方法:htmlPdf(plugin: Number, title: Number, html: Number, fileList: Number, type: Number)

示例 ​
vue
<div @click="downloadPdf" class="one">下载</div>
<div class="mainBox pdfRef" id="pdfRef">测试内容</div>
js
import html2Canvas from 'html2canvas';
import JsPDF from 'jspdf';
export default {
  methods: {
    // 下载pdf
    downloadPdf() {
      const fileName = `测评结果`;
      const fileList = document.getElementsByClassName('pdfRef');
      this.htmlPdf({
        html2Canvas: html2Canvas, // import html2Canvas from 'html2canvas';
        JsPDF: JsPDF, // import JsPDF from 'jspdf';
      }, fileName, document.querySelector('#pdfRef'), fileList);
    },
    // 下载pdf2
    downloadPdf2() {
      const fileName = `测评结果`;
      const fileList = document.getElementsByClassName('pdfRef');
      this.htmlPdf({
        html2Canvas: html2Canvas, // import html2Canvas from 'html2canvas';
        JsPDF: JsPDF, // import JsPDF from 'jspdf';
      }, fileName, document.querySelector('#pdfRef'), fileList, false, (e) => {
        // 成功
        if (e === 'htmlPdfFinish') {

        }
        // 失败
        else {
          console.error(e);
        }
      })
    },
  },
};

参数 ​

参数类型必填默认值说明
plugin.html2CanvasAny√依赖,import html2Canvas from 'html2canvas';推荐版本1.4.1。
plugin.JsPDFAny√依赖,import JsPDF from 'jspdf';推荐版本2.5.1。
titleString√下载后的文件名称。
htmlDom√Dom,例如:document.querySelector('#pdfRef')。
fileListDom×多节点,例如:document.getElementsByClassName('pdfRef')。
typeBoolean×默认纵版,true时为横版。
completefunction×完成回调。