PDF not loading in create-react-app
See original GitHub issueI’m trying to display a pdf file that gets served to my client side react app from a flask server. I send the file from flask using the send_from_directory function.
My flask server returns the pdf and I create a Uint8Array from the response.data. The result is just a message that says Loading PDF…
How can I get the pdf to load? The onSourceSuccess runs so I know my file is there, and I see the Uint8Array in my state.
var data = res.data;
var len = data.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = data.charCodeAt(i);
}
this.setState({ pdf: bytes, error: '' });
Then in my render() method I have…
content = (
<div>
<Document
file={{ data: pdf }}
onLoadSuccess={this.onPDFLoad}
onSourceSuccess={e => console.log('Source Success')}>
<Page pageNumber={pageNumber} />
</Document>
<p> Page {pageNumber} of {numPages}</p>
</div>
);
These are the imports I’m using…
import { Document, Page } from 'react-pdf/dist/entry.webpack';
// import { Page, Document } from 'react-pdf';
import { pdfjs } from 'react-pdf';
import 'react-pdf/dist/Page/AnnotationLayer.css';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${
pdfjs.version
}/pdf.worker.js`;
I’ve gotten help mainly from this Stack Overflow page. https://stackoverflow.com/questions/45596329/display-pdf-in-reactjs
“react-scripts”: “3.0.0”, “react-pdf”: “^4.1.0”,
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:7 (3 by maintainers)
Top Results From Across the Web
react-pdf library is not loading the PDF document
import React from "react"; import { Document, pdfjs } from "react-pdf"; pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/ ...
Read more >Displaying PDF in React app - Level Up Coding
I want to show you an easy and simple way to display PDF files using React in the browser. I will be using...
Read more >react-pdf-highlighter - npm
Start using react-pdf-highlighter in your project by running `npm i react-pdf-highlighter`. ... Create React App example is available in .
Read more >React-pdf
React -pdf. React renderer for creating PDF files on the browser and server. Try it out! 1. Install React and react-pdf. Starting ...
Read more >How to Build a React.js PDF Viewer with react-pdf - PSPDFKit
It's used to display existing PDFs. We'll look at how to use this library for the purpose of creating a PDF viewer. react-pdf...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a similar situation.
“react-pdf”: “^4.1.0”, “react-scripts”: “^3.1.1” (as well as Electron)
Currently code looks like this: I currently have the pdf.worker.js copied to my output folder. There is no difference if I call it from CDN or not, using the code in the documentation.
@wojtekmaj & @webdevbyjoss : onSourceError does not fire onSourceSuccess fires with no output? onLoadError does not fire. onLoadSuccess does not fire
The view just states “Loading PDF…” with no errors:
In the console I get the warning:
But from what I’ve been reading it should work, even with that warning?
Any ideas?
Hi, I have an issue similiar to this one which @DanielJackson-Oslo reported. I’ve debugged a little and I see that this promise is unresolved https://github.com/wojtekmaj/react-pdf/blob/master/src/Document.jsx#L116. Nothing after line 116 is not called - neither
then
norcatch
norfinally
.Interesting is fact, that a few weeks ago everything worked well and component’s code has’nt changed.