简体   繁体   English

在 UInt8Array 的 React 中显示 PDF 时出现问题(react-pdf)

[英]Problem displaying PDF in React from UInt8Array (react-pdf)

I'm trying to build a PDF viewer in React, I've already built the backend to upload the files and fetch the buffer data.我正在尝试在 React 中构建一个 PDF 查看器,我已经构建了后端来上传文件并获取缓冲区数据。 But I'm having some problems with react-pdf, as I can't seem to serve the right type of data.但是我在使用 react-pdf 时遇到了一些问题,因为我似乎无法提供正确类型的数据。 This is the code I've written:这是我写的代码:

const [data, setData] = useState();
  useEffect(() => {
    fetch("http://localhost:5000/files/pdf-test.pdf")
      .then((res) => res.text())
      .then((res) => setData(new Buffer(res, "binary")));
  });

  return (
    <>
      <Document file={{ data: data }} />
    </>
  );

This is one of the few tries I've made, in this one the backend serves the binary data of the file, and if we console log the last .then we can see that I'm serving the Document a UInt8Array, which is the recommended data format:这是我为数不多的尝试之一,在这个后端提供文件的二进制数据,如果我们控制台记录最后一个.then我们可以看到我正在为Document提供一个 UInt8Array,这是推荐数据格式:

数据

Apart from the code in the image, I've also tried it with binary and an ArrayBuffer, but still no results, changing both the backend and frontend code.除了图像中的代码,我还用二进制和 ArrayBuffer 尝试过,但仍然没有结果,同时更改了后端和前端代码。 The error I get is:我得到的错误是: 在此处输入图像描述

TL;DR : I'm trying to display PDF files in React with react-pdf using their buffer but I can't manage to do it. TL;DR :我正在尝试使用他们的缓冲区在 React 中显示 PDF 个文件,但我无法做到。 I have already made a backend to upload files (express-fileupload) and store them so that their data can be fetched.我已经做了一个后端来上传文件(express-fileupload)并存储它们以便可以获取它们的数据。

Thank u for helping, and I'm open to other approaches to the problem谢谢你的帮助,我愿意接受其他解决问题的方法

For RCA (react-create-app) you need to config the worker in order to view your PDF file.对于 RCA (react-create-app),您需要配置 worker 才能查看您的 PDF 文件。

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

Then configure it like this:然后像这样配置它:

pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

Usage:用法:

const [uint8Arr, setUint8Arr] = useState();

function getUint8Array() {
 let reader = new FileReader();

            // reader.readAsDataURL(selectedFile); base64
            reader.readAsArrayBuffer(selectedFile);
            reader.onloadend = async (e) => {
                if ( e.target?.result instanceof ArrayBuffer) {
                    const uint8Array = new Uint8Array(e.target.result);
                    setUint8Arr(uint8Array) <---- 

                    // more callbacks(file.name, Buffer.from(new Uint8Array(target.result)));
                }
            };
}
<Document file={{
            data: uint8Arr <----
        }} onLoadSuccess={() => console.log('SUCCESS LOAD')}>
            <Page pageNumber={1} />
        </Document>

Hope that this will fix your issue.希望这能解决您的问题。

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

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