0

I'm using react-scripts@5.0.1 and react-pdf@5.7.2. No matter what I try I can't get it to load a pdf.

Originally I had the following setup, which was working with react-scripts start, but would fail on build with the following error: Module not found: Error: Can't resolve 'react-pdf/dist/esm/entry.webpack'

import { Document, Page } from 'react-pdf/dist/esm/entry.webpack';

Apparently CRA 5 doesn't play nice and you have to do extra steps to copy over some worker file, per the standard instructions here

So I followed the steps mentioned there, installed pdfjs-dist and used the script in the sample directory here to copy the worker file to my public folder.

So now I have a pdf.worker.js file in my public directory and my code now looks like this:

import { Document, Page, pdfjs } from "react-pdf";
pdfjs.GlobalWorkerOptions.workerSrc = 'pdf.worker.js';

With this change it won't even load with react-scripts start anymore, where before it had.

I now see this error in my browser console:

Deprecated API usage: No "GlobalWorkerOptions.workerSrc" specified

I came across this stackoverflow post, where someone is having similar issues.

I tried multiple answers posted there, including this one, and this comment that says to also copy the worker.js.map file. Neither of them worked.

I'm at a loss as to what to do now.

EDIT: Ended up giving up, if anyone posts an actual solution I'll give it a shot. For context I was using @react-pdf/renderer to generate the pdf document, and was wrapping react-pdf with it's `BlobProvider like in this example

Instead of all that, I opted to just include a download link for my users using PDFDownloadLink from @react-pdf/renderer instead of rendering the pdf in browser, which would have been a lot cooler.

0 Answers0