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
I think you need to import pdfjs with require based on this discussion here and this example here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With