Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create react app - how to copy pdf.worker.js file from pdfjs-dist/build to your project's output folder?

Since I can't use browser's pdf viewer in the network where the app is going to be used, I am testing a react-pdf package for loading PDF's with React. I have made a component where I am sending a url of my PDF that I get from backend:

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

const PDFViewer = ({url}) => {
  const [numPages, setNumPages] = useState(null);
  const [pageNumber, setPageNumber] = useState(1);

  function onDocumentLoadSuccess({ numPages }) {
    setNumPages(numPages);
  }
 function onLoadError(error) {
   console.log(error);
 }

 function onSourceError(error) {
   console.log(error);
 }

  return (
    <div>
      <Document
        file={window.location.origin + url}
        onLoadSuccess={onDocumentLoadSuccess}
        onLoadError={onLoadError}
        onSourceError={onSourceError}
      >
        {[...Array(numPages).keys()].map((p) => (
          <Page pageNumber={p + 1} />
        ))}
      </Document>
    </div>
  );
};

export default PDFViewer;

But, on opening the PDFViewer I get an error

Error: Setting up fake worker failed: "Cannot read property 'WorkerMessageHandler' of undefined"

In documentation it says that you should set up service worker and that the recommended way is to do that with CDN:

import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`;

But, I can't use CDN links for my project, and in the documentation it also says:

Create React App uses Webpack under the hood, but instructions for Webpack will not work. Standard instructions apply. Standard (Browserify and others) If you use Browserify or other bundling tools, you will have to make sure on your own that pdf.worker.js file from pdfjs-dist/build is copied to your project's output folder.

There are no instructions on how to do that with create-react-app. How can I set this up locally then?

like image 655
Leff Avatar asked Jan 15 '21 16:01

Leff


People also ask

How do I create a react component PDF?

Generating a pdf on the client side is a 3 step process :Convert the DOM into svg. Convert the svg into png. Convert the png into pdf.

What is PDF worker JS?

PDF. js is an open-source JavaScript PDF viewer that renders PDF using web standards-compliant HTML5. Primarily seen in Mozilla Firefox's as the built-in PDF viewer, PDF. js also serves as an easy way for developers and integrators to embed PDF viewing capabilities in a web app or server.


1 Answers

Install pdfjs-dist

import { Document, Page, pdfjs } from "react-pdf";
import pdfjsWorker from "pdfjs-dist/build/pdf.worker.entry";

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

Reference: https://github.com/mozilla/pdf.js/issues/8305

like image 67
lissettdm Avatar answered Sep 19 '22 17:09

lissettdm