0

Hi I am trying to render pdf using react-pdf in my next app, but facing the following issue:

SyntaxError: Named export 'PDFDataRangeTransport' not found. The requested module 'pdfjs-dist' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using:

import pkg from 'pdfjs-dist';
const { PDFDataRangeTransport } = pkg;


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


export const CaseFile = (props: { caseId: any, doctors: Array<any>, stateTracker: any }) => {

    const [records, setRecords] = useState([])
    const [caseDetails, setCaseDetails] = useState<any>()
    const [auditEntries, setAuditEntries] = useState<Array<any>>([])
    const [numPages, setNumPages] = useState(null);

    function onDocumentLoadSuccess({numPages: nextNumPages}) {
        setNumPages(nextNumPages);
    }


    pdfjs.GlobalWorkerOptions.workerSrc = new URL(
        'pdfjs-dist/build/pdf.worker.min.js',
        import.meta.url,
    ).toString();

    function getDischargeSummaryView() {
            return <div className="p-4">
                      <Document file={"/api/s3/download/?file=something.pdf"}
                                          onLoadSuccess={onDocumentLoadSuccess}>
                                    {Array.apply(null, Array(numPages))
                                        .map((x, i) => i + 1)
                                        .map((page, i) => <Page key={i} pageNumber={page} renderTextLayer={false}
                                                                renderAnnotationLayer={false}/>)}
                                </Document>
                    </div>         
   }
   return (
        <>
            {getDischargeSummaryView()}
        </>
    )
}
```

I updated the next.config.js

```
webpack(config, options) {
        config.module.rules.push({
                test: /\.node/,
                use: 'raw-loader',
            },
            {
                test: /\.mp3$/,
                use: {
                    loader: 'file-loader',
                },
            });
        return config;
    },


While paging the load the issue aaoears, However if i dont use the Document and let the page render and add the Document again, it renders correctly.
toyota Supra
  • 3,181
  • 4
  • 15
  • 19

2 Answers2

0

See https://github.com/wojtekmaj/react-pdf/discussions/1577

If you downgrade to version 7.2.0 it should work without issues.

benzler
  • 1
  • 1
0

This bug has been fixed in the newest version v7.3.3