Closed muhammad-asad-26 closed 5 years ago
As clearly stated in https://github.com/mozilla/pdf.js/blob/master/.github/CONTRIBUTING.md, code-snippets are not useful and a complete and runnable example needs to be provided; as-is the issue is incomplete.
Closing until the required information is provided.
Hi I'm trying to use pdfjs-dist in react app but its not working. I'm getting following error:
Uncaught SyntaxError: Unexpected token < Error: Setting up fake worker failed: "Cannot read property 'WorkerMessageHandler' of undefined". at pdf.js:10999
Configuration:
Steps to reproduce the problem:
My Code:
class Page extends Component { state = { status: "N/A", page: null, width: 0, height: 0 };
shouldComponentUpdate(nextProps, nextState) { return ( this.props.pdf !== nextProps.pdf || this.state.status !== nextState.status ); }
componentDidUpdate(nextProps) { this._update(nextProps.pdf); }
componentDidMount() { const { pdf } = this.props; this._update(pdf); }
setCanvasRef = canvas => { this.canvas = canvas; };
_update = pdf => { if (pdf) { this._loadPage(pdf); } else { this.setState({ status: "loading" }); } };
_loadPage(pdf) { if (this.state.status === "rendering" || this.state.page !== null) return;
}
_renderPage(page) { let { scale } = this.props; let viewport = page.getViewport(scale); let { width, height } = viewport; let canvas = this.canvas; let context = canvas.getContext("2d"); canvas.width = width; canvas.height = height;
}
render() { let { width, height, status } = this.state;
} }
export { Page };
import React from "react"; import { Page } from "./Page";
const Viewer = ({ pdf, ...props }) => { const numPages = pdf ? pdf._pdfInfo.numPages : 0;
if (pdf) { return (
}
return null; };
export { Viewer };
import React, { Component } from "react"; import * as PdfJs from "pdfjs-dist"; import { Viewer } from "./Helper/Viewer"; import "./index.css";
class PDFViewer extends Component { state = { pdf: null, scale: 1.2 };
componentDidMount() { PdfJs.getDocument(this.props.url) .then(pdf => { this.setState({ pdf }); }) .catch(error => { console.log(error); }); }
render() { const { pdf, scale } = this.state; return (
} }
export { PDFViewer };