根据后端给的接口把已经处理好的word转成pdf

227 阅读1分钟

根据后端给的接口把已经处理好的word转成pdf

image.png sp20220810_141522_242.png

8973d464f29d58d82cf6622ee66b5d6.jpg

附1:# vue中下载pdf文件

blog.csdn.net/Tttrycatch/…

附2:# vue下载pdf为空问题解决

blog.csdn.net/weixin_4392…

过程中遇到跨域问题,前后端都需进行解决

    const out = doc.getZip().generate({
      type: "blob",
      mimeType:
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    });
    // Output the document using Data-URI
    // saveAs(out, `导出-${this.detailBase.invProjectBaseInfo.name}.docx`);
    const formdata = new FormData();
    // console.log(out);
    formdata.append("file", out, "xx.docx");
    // console.log(formdata);
    // let filename = `导出-${this.detailBase.invProjectBaseInfo.name}.pdf`;
    // $.ajax({
    //   url: "/pdfapi/api/pdfConvert/convertAndDownload",
    //   type: "POST",
    //   Headers: {
    //     "Access-Control-Allow-Origin": "*",
    //   },
    //   contentType: false,
    //   data: formdata,
    //   // contentType: false,
    //   processData: false,
    //   xhrFields: { responseType: "blob" },
    //   // dataType: "blob",
    //   success: function (res) {
    //     // debugger;
    //     console.log(res);
    //     const blob = new Blob([res], {
    //       // type: "application/pdf;chartset=UTF-8",
    //     });
    //     const a = document.createElement("a");
    //     const URL = window.URL || window.webkitURL;
    //     const herf = URL.createObjectURL(blob);
    //     a.href = herf;
    //     // 这里后台不给文件名字需要自己写
    //     a.download = filename;
    //     document.body.appendChild(a);
    //     a.click();
    //     document.body.removeChild(a);
    //     window.URL.revokeObjectURL(herf);
    //   },

    //   error: function (res) {
    //     // debugger;
    //     console.log(res);
    //   },
    // });

    axios
      .post(
        "http://124.222.176.110:10086/api/pdfConvert/convertAndDownload",
        formdata,
        {
          "Content-type": "multipart/form-data",
          responseType: "blob",
        }
      )
      .then((res) => {
        console.log(res.data);
        // debugger;
        const blob = new Blob([res.data], {
          // type: "application/pdf;chartset=UTF-8",
        });
        const a = document.createElement("a");
        const URL = window.URL || window.webkitURL;
        const herf = URL.createObjectURL(blob);
        a.href = herf;
        // 这里后台不给文件名字需要自己写
        a.download = `${this.detailBase.invProjectBaseInfo.name}.pdf`;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(herf);
      });
  });