wojtekmaj / react-pdf

Display PDFs in your React app as easily as if they were images.
https://projects.wojtekmaj.pl/react-pdf
MIT License
9.28k stars 878 forks source link

Document options not working #1529

Closed samuliasmala closed 1 year ago

samuliasmala commented 1 year ago

Before you start - checklist

Description

In version 7.0.3 verbosity was added to options prop in Document component. However adding that option will throw an error.

Steps to reproduce

Use verbosity option in Document options:

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

<Document
  file={data}
  options={{ verbosity: pdfjs.VerbosityLevel.ERRORS }}
>
  <Page pageNumber={1} />
</Document>

Expected behavior

The document is displayed.

Actual behavior

I get TypeError: Cannot perform Construct on a detached ArrayBuffer error:

image

Additional information

If the options={{ verbosity: pdfjs.VerbosityLevel.ERRORS }} is removed everything works nicely.

Environment

wojtekmaj commented 1 year ago

Please don't inline options prop value, or memoize it if you absolutely have to specify it inside render function. It's likely your error will be gone. Refer to official documentation and samples for examples.

samuliasmala commented 1 year ago

Yes, defining the options prop outside of the render function indeed fixed the problem! Thanks. Maybe the documentation should be updated that it's not just a performance reason to define the options object outside of the React component but it's actually mandatory for it to work?

wojtekmaj commented 1 year ago

I've recently made it clearer to guide people on the right track, but perhaps it could be even better indeed. Glad it resolved your problem though.