vue導(dǎo)出當(dāng)前頁pdf

getPdf.js

import html2Canvas from 'html2canvas';
import JsPDF from 'jspdf';
  /**
   * @description: 導(dǎo)出pdf
   * @param {String} id 攜帶跳轉(zhuǎn)的id參數(shù) 
   * @return:
   */
  async getPdf(_this) {
    let userAgent = navigator.userAgent;
    // 判斷瀏覽器內(nèi)核是否是是否是火狐或IE
    if (userAgent.indexOf('Firefox') > -1 || !!window.ActiveXObject || 'ActiveXObject' in window) {
      alert('PDF導(dǎo)出暫不支持您的瀏覽器,請更換谷歌chrome內(nèi)核瀏覽器');
      return;
    }
    _this.pdfExportLoading = true;
    var title = _this.htmlTitle;
    const pdfDom = document.querySelector('#pdfDom');
    // mainFrame = document.querySelector('#mainFrame'),
    // const iframeCanvas = await html2Canvas(mainFrame);
    // pdfDom.appendChild(iframeCanvas);

    _this.$nextTick(() => {
      html2Canvas(pdfDom, {
          useCORS: true
         }
        ).then(function (canvas) {
          // pdfDom.removeChild(iframeCanvas);
          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/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 -= pageHeight;
              position -= 841.89;
              if (leftHeight > 0) {
                PDF.addPage();
              }
            }
          }
          PDF.save(title + '.pdf');
          _this.pdfExportLoading = false;
        }
        );
    });
  }

組件中標(biāo)注某個元素 id="pdfDom" 此標(biāo)注元素做為html2Canvas方法中的第一個參數(shù),
pdfExportLoading、htmlTitle導(dǎo)出文件名

<div class="table-page-search-wrapper" id="pdfDom">
....

  data() {
    return {
      htmlTitle:"",
      pdfExportLoading: false
}
}

調(diào)用 傳入當(dāng)前實例對象 至于方法掛在哪 自由決定

getPdf(this)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容