Skip to content
Overriding Dependency

@react-pdf-kit/viewer@^2.0.0 requires pdfjs-dist as a peer dependency with a default version of 5.4.530. This means you do not need to manually install pdfjs-dist unless you want to use a different version.

This guide walks you through overriding the default pdfjs-dist version in two steps:

  1. Install the version you need.
  2. Configure your specific framework to use it.

Here is how you can install package pdfjs-dist with the latest version which is above 5.4.530.

Before configuring any framework, you must first install pdfjs-dist with the version you want to use. This step is required regardless of which framework you use.

  1. Install pdfjs-dist with the version you want to use:

    Terminal window
    bun add pdfjs-dist
  2. Ensure the installed version is used by adding an override in your package.json:

    package.json
    {
    "overrides": {
    "pdfjs-dist": "the installed version"
    }
    }

After installing pdfjs-dist, pick the section below that matches your project’s framework and follow the additional configuration steps.

  1. It’s important to complete Step 1 of pdfjs-dist installation before proceeding

  2. Install package copy-webpack-plugin:

    Terminal window
    bun add copy-webpack-plugin --dev
  3. Add configurations to webpack.config.js:

    webpack.config.js
    const CopyPlugin = require('copy-webpack-plugin')
    module.exports = {
    // ...config
    plugins: [
    new CopyPlugin({
    patterns: [
    {
    from: 'node_modules/pdfjs-dist/legacy/build/pdf.worker.min.mjs',
    to: 'pdf.worker.min.mjs'
    }
    ]
    })
    /// ...other plugins
    ]
    }
  4. Add /pdf.worker.min.mjs to the workerUrl prop in the RPConfig component:

    src/App.jsx
    import { RPConfig } from '@react-pdf-kit/viewer'
    import React from 'react'
    function App() {
    return (
    <div>
    <RPConfig workerUrl={'/pdf.worker.min.mjs'}>
    ...
    </RPConfig>
    </div>
    )
    }
    export default App
  1. It’s important to complete Step 1 of pdfjs-dist installation before proceeding

  2. Add configurations to vite.config.js or vite.config.ts:

    vite.config.ts
    import { defineConfig } from 'vite'
    export default defineConfig({
    // ...config
    optimizeDeps: {
    include: ['pdfjs-dist']
    // ...
    }
    })
  3. Add the worker URL to the workerUrl prop in the RPConfig component:

    src/App.jsx
    import { RPConfig } from '@react-pdf-kit/viewer'
    import workerSrc from 'pdfjs-dist/build/pdf.worker.min.mjs?url'
    function AppPdfViewer() {
    return (
    <RPConfig workerUrl={workerSrc}>
    ...
    </RPConfig>
    )
    }
    export default AppPdfViewer
  1. It’s important to complete Step 1 of pdfjs-dist installation before proceeding

  2. Add the worker URL to the workerUrl prop in the RPConfig component:

    app/components/AppProviders.jsx
    import { RPConfig } from '@react-pdf-kit/viewer'
    import React from 'react'
    const workerSrc = new URL(
    "pdfjs-dist/build/pdf.worker.min.mjs",
    import.meta.url,
    ).toString();
    function AppProviders({ children, ...props }) {
    return (
    <RPConfig workerUrl={workerSrc} {...props}>
    {children}
    </RPConfig>
    )
    }
    export default AppProviders
  1. It’s important to complete Step 1 of pdfjs-dist installation before proceeding

  2. Add configurations to next.config.js or next.config.ts:

    next.config.js
    const path = require('path')
    /** @type {import('next').NextConfig} */
    const nextConfig = {
    // Config options here
    webpack: (config, { webpack }) => {
    config.plugins.push(
    new webpack.NormalModuleReplacementPlugin(/^pdfjs-dist$/, (resource) => {
    resource.request = path.join(__dirname, './node_modules/pdfjs-dist/webpack')
    })
    )
    return config
    }
    }
    module.exports = nextConfig;
  3. Add /pdf.worker.min.mjs to the workerUrl prop in the RPConfig component:

    app/components/AppProviders.jsx
    import { RPConfig } from '@react-pdf-kit/viewer'
    import React from 'react'
    function AppProviders({ children, ...props }) {
    return (
    <RPConfig workerUrl='/pdf.worker.min.mjs' {...props}>
    {children}
    </RPConfig>
    )
    }
    export default AppProviders

This section applies only if you are using Vite with TypeScript. The ?url import suffix used in the Vite configuration above may cause a TypeScript error.

For TypeScript, you may get an error Cannot find module 'pdfjs-dist/build/pdf.worker.min.mjs?url' or its corresponding type declarations. from import import workerSrc from 'pdfjs-dist/build/pdf.worker.min.mjs?url'.

To resolve this issue, you can add file .d.ts (e.g., global.d.ts) with the content below to your project

global.d.ts
declare module '*?url' {
const content: string
export default content
}