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

Working with the PDF Viewer in NextJS 13 and App folders: Module not found "canvas" #43

Open
mtreilly opened this issue Oct 29, 2022 · 3 comments

Comments

@mtreilly
Copy link

Comment:
Not sure where to post this, but thought it might be helpful to others

Issue:

When using the PDF viewer inside a page of the new app folder layout, regardless if you use "use client" at the top of the document, the file will still be run on the server. Causing the pdfjs code to require("canvas"), which throws an error saying module not found.

Solution:

For the component containing the PDF viewer, disable SSR and use a dynamic import instead.
Run import from "use client" component

Example

Display.tsx

"use client";

import React from "react";

import dynamic from "next/dynamic";

const DisplayPDF = dynamic(() => import("./DisplayPDF"), {
  ssr: false,
});

export default function Display() {
  return (
      <div>
        <DisplayPDF />
      </div>
    );
}

DynamicPDF.tsx

"use client";

import React from "react";
import { Viewer, Worker } from "@react-pdf-viewer/core";
import { defaultLayoutPlugin } from "@react-pdf-viewer/default-layout";

export default function DisplayPDF() {
  const defaultLayoutPluginInstance = defaultLayoutPlugin();

  return (
    <Worker workerUrl="https://unpkg.com/pdfjs-dist@2.15.349/build/pdf.worker.js">
      <div style={{ height: "750px" }}>
        <Viewer
          fileUrl={"./in-context.pdf"}
          // fileUrl="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
          plugins={[defaultLayoutPluginInstance]}
        />
      </div>
    </Worker>
  );
}
@Tekstar47
Copy link

Ran into the same issue but the above fix didn't work for me. Do you mind sharing a dummy repo so I can take a closer look?

@riccardolinares
Copy link

It solved my problem!

@abugi
Copy link

abugi commented Dec 31, 2023

This solved my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants