wojtekmaj / react-pdf

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

using PDF version 9.0.0,Uncaught TypeError: Object.defineProperty called on non-object #1813

Closed whizbz11 closed 1 month ago

whizbz11 commented 1 month ago

Before you start - checklist

Description

image

Steps to reproduce

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

pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`;
interface IProps {
    url: string;
}
const opt =  {
    cMapUrl: `${window.location.origin}/cmaps/`,
    cMapPacked: true,
    isEvalSupported: false,
}

const PageRender = ({url}: IProps) => {
    const [numPages, setNumPages] = useState(1);
    const pageRef = useRef<HTMLDivElement>(null);

    const onDocumentLoadSuccess = useCallback(
        (document) => {
            const {numPages: nextNumPages} = document;
            setNumPages(nextNumPages);
        },
        []
    );

    return (
        <div
            className={'pageReaderWrap'}
            ref={pageRef}
        >
            <Document
                file={url}
                key={url}
                onLoadSuccess={onDocumentLoadSuccess}
                options={opt}
            >
                {Array.from(new Array(numPages), (el, index) => {
                    return (
                        <Page
                            key={`page_${index + 1}`}
                            renderTextLayer={false}
                            loading={''}
                            renderAnnotationLayer={false}
                            pageNumber={index + 1}
                        />
                    );
                })}
            </Document>
        </div>
    );
};

export default PageRender;

Expected behavior

Expected to work properly without errors

Actual behavior

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

pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`;
interface IProps {
    url: string;
}
const opt =  {
    cMapUrl: `${window.location.origin}/cmaps/`,
    cMapPacked: true,
    isEvalSupported: false,
}

const PageRender = ({url}: IProps) => {
    const [numPages, setNumPages] = useState(1);
    const pageRef = useRef<HTMLDivElement>(null);

    const onDocumentLoadSuccess = useCallback(
        (document) => {
            const {numPages: nextNumPages} = document;
            setNumPages(nextNumPages);
        },
        []
    );

    return (
        <div
            className={'pageReaderWrap'}
            ref={pageRef}
        >
            <Document
                file={url}
                key={url}
                onLoadSuccess={onDocumentLoadSuccess}
                options={opt}
            >
                {Array.from(new Array(numPages), (el, index) => {
                    return (
                        <Page
                            key={`page_${index + 1}`}
                            renderTextLayer={false}
                            loading={''}
                            renderAnnotationLayer={false}
                            pageNumber={index + 1}
                        />
                    );
                })}
            </Document>
        </div>
    );
};

export default PageRender;

Additional information

No response

Environment

tbpzf commented 1 month ago

This is related to the eval- devtool in webpack config, change it to false or something other than eval-. It works for me.

wojtekmaj commented 1 month ago

Thanks @tbpzf! I can confirm this fixes the issue. Added that fix in 8307a4f1d89c80571358506b93afc3f4af6dc815 to our official samples.

helloworldcyj commented 3 weeks ago

If you are using nextjs: https://github.com/vercel/next.js/discussions/21425