简体   繁体   English

下载从 ERP (weclapp) 的 API 调用中收到的 PDF,该 PDF 是用 Javascript 进行 FlateDecoded

[英]Download a PDF received from an API call from ERP (weclapp) that is FlateDecoded in Javascript

I intend to receive a pdf to directly download it from an API call.我打算接收 pdf 以直接从 API 调用下载它。 What I got back, looks like the following:我返回的内容如下所示:

%PDF-1.4
%����
4 0 obj
<</Filter/FlateDecode/Length 983>>stream
x��Xo�0Է<ѭC�:�H�]`
����ڍ��֕n"$�

What do I need to do to download this?我需要做什么才能下载这个? I am working in vuejs/axios as frontend and intend to use express as backend.我在 vuejs/axios 作为前端工作,并打算使用 express 作为后端。

I found a similar looking problem here, but without solution: Download PDF file from api using javascript IE9 I implemented the code, but the resulting pop-up simply stays white我在这里发现了一个类似的问题,但没有解决方案: 使用 javascript IE9 从 api 下载 PDF 文件我实现了代码,但结果弹出窗口只是保持白色

Generally it seems to be a pdf with a FlateDecode-filter.通常它似乎是带有 FlateDecode 过滤器的 pdf。

I have this utility to download files from an API url.我有这个实用程序可以从 API url 下载文件。

import axios from "axios";

/**
 * utility to download a File from an api url.
 * @param config (optional) config for axios
 */
export default function download(url, config) {
  return axios.get<Blob>(url, {
    ...config,
    responseType: "blob"
  }).then(response => {
    // get filename from content-disposition header
    const contentDisposition = response.headers["content-disposition"];
    const fileName = contentDisposition.match(/\bfilename=([^;]*)/)[1].trim();
    
    //"save" file to hard disk
    const link = document.createElement("a");
    link.href = URL.createObjectURL(response.data);
    link.setAttribute("download", fileName);
    document.body.appendChild(link);
    link.click();

    // cleanup
    setTimeout(() => {
      link.parentNode.removeChild(link);
    }, 3000);

    return response;
  })
}

So I found this working answer in this thread: https://gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743所以我在这个线程中找到了这个有效的答案: https : //gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743

It seems like there is a ton of different ways to approach this.似乎有很多不同的方法可以解决这个问题。

  await this.$axios( this.api_url, 
    {method: 'GET', responseType: 'blob', headers: {"AuthenticationToken": this.api_token} }
    ).then(response => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    // works in IE11
    if (typeof window.navigator.msSaveBlob === 'function') {
      window.navigator.msSaveBlob(
        response.data,
        `${filename}.pdf`
      );
    } else {
      link.setAttribute('download', `filename.pdf`);
      document.body.appendChild(link);
      link.click();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM