Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught (in promise) Error when using PDF.js viewer with React.js library?

Tags:

reactjs

pdf.js

I installed pdfjs-dist and tried to import it and use it with react using the following component:

import React, { Component } from 'react'
import * as pdfjs from 'pdfjs-dist'

class App extends Component {
  componentDidMount() {

    pdfjs.getDocument('./sample.pdf').promise.then(doc => console.log(doc.numPages))
  }

  render() {
    return <div>test pdf</div>
  }
}

export default App

but I'm getting the next error at console

Uncaught (in promise) Error: Setting up fake worker failed: "Cannot read property 'WorkerMessageHandler' of undefined".

I tried also to change the import statement to :

import pdfjs from 'pdfjs-dist/webpack'

but I got this error:

Uncaught ReferenceError: window is not defined

like image 830
Zuhair Taha Avatar asked Oct 27 '25 09:10

Zuhair Taha


1 Answers

I think you need to import pdfjs with require based on this discussion here and this example here

like image 74
Trevor Johnson Avatar answered Oct 29 '25 00:10

Trevor Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!