【问题标题】:How to fetch pdf file and display it using react-pdf library如何使用 react-pdf 库获取 pdf 文件并显示它
【发布时间】:2021-10-26 22:24:40
【问题描述】:

我正在尝试从我的后端获取 pdf 文件并将其显示在页面上。我正在使用 react-pdf 库 https://www.npmjs.com/package/react-pdf 。但是我遇到了一个错误

文件中的参数无效,需要 Uint8Array、字符串或 参数对象

你能帮帮我吗

import { Document, Page } from 'react-pdf';

<Document file={() => dispatch(downloadAgreement())}>
  <Page pageNumber={pageNumber} />
</Document>

export const downloadAgreement = () => (dispatch) => {
  return axios(`https://test-zdc/pdf`, {
     method: 'GET',
     responseType: 'blob' 
  }).then((res) => {
    const file = new Blob([res.data], { type: 'application/pdf' });
    const fileURL = URL.createObjectURL(file);
    return fileURL;
  });
};

【问题讨论】:

  • 你能把它放在codesandbox上吗?

标签: reactjs blob react-pdf


【解决方案1】:

在自述文件中不清楚 file 期望什么,我认为你需要一个像

这样的对象
return {url: fileURL}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多