Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to async load PDF.js similar to pdf.worker.js? PDF.js is about 200kb, which is too large for me. #1710

Open
2 tasks done
margintopt opened this issue Feb 4, 2024 · 7 comments
Labels
enhancement New feature or request fresh

Comments

@margintopt
Copy link

margintopt commented Feb 4, 2024

Before you start - checklist

  • I understand that React-PDF does not aim to be a fully-fledged PDF viewer and is only a tool to make one
  • I have checked if this feature request is not already reported

Description

// pdj.ts in react-pdj

import * as pdfjsModule from 'pdfjs-dist';

const pdfjs = (
'default' in pdfjsModule ? pdfjsModule['default'] : pdfjsModule
) as typeof pdfjsModule;

export default pdfjs;

Proposed solution

<script src="//mozilla.github.io/pdf.js/build/pdf.mjs" type="module"></script>

external script load pdf.js

Alternatives

No response

Additional information

No response

@margintopt margintopt added the enhancement New feature or request label Feb 4, 2024
@wojtekmaj
Copy link
Owner

There's no direct way of doing this in v7. It will be possible with v8 as pdf.js 4.x supports it. Until then, perhaps lazy load the entire component?

@margintopt
Copy link
Author

There's no direct way of doing this in v7. It will be possible with v8 as pdf.js 4.x supports it. Until then, perhaps lazy load the entire component?

Can I clone the library myself and then use webpack external to reduce the size of the pdfjs part?

@wojtekmaj
Copy link
Owner

I guess you could! I don't know much about this process, but react-pdf is open source and everything is allowed, feel welcome to experiment!

Copy link
Contributor

github-actions bot commented May 6, 2024

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this issue will be closed in 14 days.

@github-actions github-actions bot added the stale label May 6, 2024
@wojtekmaj wojtekmaj added fresh and removed stale labels May 6, 2024
@nikischin
Copy link

I implemented the complete component as an asynchronous import within a Suspense and it works well and keeps my package size small. Not sure if this satisfies your needs though let me know if you need some code sample.

@StevenRasmussen
Copy link

@nikischin - Any chance you're able to share that code?

@nikischin
Copy link

nikischin commented Jun 8, 2024

Sure! Basically like this:

const PDFViewer = React.lazy(()=> import('./PDFViewer'));
export interface URLPDFViewerProps { url?: string, orientation?: 'portrait' | 'landscape' }

export default function URLPDFViewer (props: URLPDFViewerProps) {
    return (
        <Suspense fallback={<Spinner animation='border' />}>
            <PDFViewer {...props} />
        </Suspense>
    );
}
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
    'pdfjs-dist/build/pdf.worker.min.mjs',
    import.meta.url,
).toString();

export interface URLPDFViewerProps { url?: string, orientation?: 'portrait' | 'landscape' }

export default function PDFViewer ({ url, orientation }: URLPDFViewerProps) {
    return (
        <Document
            file={url}
            loading={<div><Spinner animation='grow' /></div>}
        >
            <Page
                height={orientation === 'portrait' ? 360 : undefined}
                width={orientation === 'portrait' ? undefined : 360}
            />
        </Document>
    );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fresh
Projects
None yet
Development

No branches or pull requests

4 participants