I am trying to load a pdf file into my project, but I am unable to see it. It just keeps showing 'Loading PDF...'
I have added pdfjs
web-worker as mentioned in some of their github repo issues, but still no change. I tried building the page by creating a new project suing create-react-app
and it seems to be working fine.
import React, { PureComponent } from "react";
import { Document, Page, pdfjs } from "react-pdf/dist/entry.webpack";
import printJS from "print-js";
import requiredFile from "./pdfdemo.pdf";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${
pdfjs.version
}/pdf.worker.js`;
export default class PdfViewer extends PureComponent {
state = {
numPages: null,
pageNumber: 1,
rotate: 0,
scale: 1
};
onDocumentLoadSuccess = ({ numPages }) => {
console.log('this function was triggered')
this.setState({ numPages });
};
render() {
const { pageNumber, numPages, scale, rotate } = this.state;
return (
<React.Fragment>
<div id="ResumeContainer">
<div style={{ width: 600 }}>
<Document
className="PDFDocument"
file={requiredFile}
onLoadError={(error) => {
console.log("Load error", error)
}}
onSourceSuccess={() => {
console.log("Source success")
}}
onSourceError={(error) => {
console.error("Source error", error)
}}
onLoadSuccess={this.onDocumentLoadSuccess}
>
{window === undefined ? <div>nothing here</div> : <Page
pageNumber={pageNumber}
height={600}
className="PDFPage PDFPageOne"
scale={scale}
/>}
</Document>
</div>
</div>
</React.Fragment>
);
}
}
The onSourceSuccess
callback seems to be firing on console logging, but none of the other callbacks fire. In the console, I can see an error stating that the window is undefined.