截图并将图片导出为pdf

 dataURItoBlob (dataURI) {
      const byteString = atob(dataURI.split(',')[1]);
      const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
      const ab = new ArrayBuffer(byteString.length);
      const ia = new Uint8Array(ab);
      for (let i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
      }
      return new Blob([ab], { type: mimeString });
    },

    // 将Blob对象转换为File对象
    blobToFile (blob, filename) {
      const file = new File([blob], filename, { type: blob.type });
      return file;
    },

    // 将Base64编码字符串转换为File对象
    base64ToFile (base64, filename) {
      const blob = this.dataURItoBlob(base64);
      return this.blobToFile(blob, filename);
    },
    // 下载报告
    downloadReport () {
      let _this = this
      let node = document.getElementById("mapDivreportForm")
      domtoimage.toPng(node, { quality: 1.0 })
        .then(function (dataUrl) {
          const base64String = dataUrl; // Base64编码字符串
          const file = _this.base64ToFile(base64String, this.statisticAnalysisForm.pdfName + '.png'); // 转换为File对象
          console.log(file, 'file')
          _this.exportPdf(file)
        });
    },
    exportPdf (dataUrl) {
      axios.post("/gj-lzz-pie/webBusiness/api/sldtjcBhxb/exportPDF",
        {
          currentPage: 1,
          pageSize: 9999,
          data: {
            code: "", //localStorage.fDatapermissionid
            shape: this.drawWkt,
            fileName: this.statisticAnalysisForm.pdfName,
            photoBase: dataUrl //file
          }
        },
        { responseType: "blob" }
      ).then(res => {
        const link = document.createElement('a')
        let blob = new Blob([res.data], { type: 'application/pdf;charset=utf-8' });
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob);
        link.download = this.statisticAnalysisForm.pdfName + '.pdf'
        link.click()
      })
    },

猜你喜欢

转载自blog.csdn.net/ANNENBERG/article/details/133859566