Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display saved pdf in mongoDB on react #754

Closed
sinamhbb opened this issue Mar 30, 2021 · 1 comment
Closed

display saved pdf in mongoDB on react #754

sinamhbb opened this issue Mar 30, 2021 · 1 comment
Labels
question Further information is requested

Comments

@sinamhbb
Copy link

sinamhbb commented Mar 30, 2021

Hello there,

I am developing a small service that tracks your job applications.
first, you create a job application and choose a PDF file from your local system.
As soon as you submit the application, react will send a POST request to the server-side on Node.js and save the data in MongoDB as follow:
image

Now for showing back the PDF react request the same files from the server using API and it will become like this:
image

The code for showing the file is as follow:
I have commented my attempts(to encode binary to base64) so I could try both ways at the same time:

import React, { useState } from "react";
import { Document, Page, pdfjs } from "react-pdf";

const ListItems = ({ items }) => {
  // attempt A =>
  var binary = items[0].cover.data;
  let base64Data = btoa(binary);

  // attempt 2 =>
  // let buff = new Buffer(items[0].cover.data);
  // let base64data = buff.toString("base64");

  // console.log(enc);
  const [numPages, setNumPages] = useState(null);
  const [pageNumber, setPageNumber] = useState(1);

  function onDocumentLoadSuccess({ numPages }) {
    setNumPages(numPages);
  }
  return (
    <>
      <div className="container">
        <Document
          file={`data:application/pdf;base64,${base64Data}`}
          onLoadSuccess={onDocumentLoadSuccess}
        ></Document>
      </div>
    </>
  );
};

in both ways, I get this: Failed to load PDF file.

Also in console it shows like this:
image

Please advise me on how to make this work.

@wojtekmaj
Copy link
Owner

This belongs to Stack Overflow, not GitHub Issues. There's no React-PDF issue here.

Regarding the console, both errors were already discussed in other issues e.g. #97.

@wojtekmaj wojtekmaj added the question Further information is requested label Mar 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants