react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

Open luokelong opened this issue 1 year ago • 7 comments

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

"@react-pdf/renderer": "^4.0.0", "next": "14.2.13",

"use client";

import { PDFViewer, Document, Page, View, Text } from "@react-pdf/renderer";

const Pdf = () => {
  return (
    <PDFViewer width="100%" height="100%">
      <Document>
        <Page size="A4">
          <View>
            <Text>Hello World!</Text>
          </View>
        </Page>
      </Document>
    </PDFViewer>
  );
};

export default Pdf;

luokelong avatar Sep 29 '24 11:09 luokelong

Same here.

Tossko avatar Oct 01 '24 23:10 Tossko

I have the same issue using the PDFDownloadLink component.

antonigiske avatar Oct 02 '24 06:10 antonigiske

Has any official paid attention to this issue?

luokelong avatar Oct 03 '24 16:10 luokelong

Same here with "@react-pdf/renderer": "3.4.4", "next": "14.2.12",

lorena-caia avatar Oct 07 '24 14:10 lorena-caia

If anyone is having this issue, there are two temporary fixes:

  • Downgrade to [email protected] and to make it work for now
  • Put "use client" in the top of the page.tsx file if possible ( not possible if you have any server logic in the page.tsx file )

These are just patches to make it work for now though

typesafeui avatar Oct 09 '24 09:10 typesafeui

The issue is related to a regression in a next.js version or a bug in this package?

bryanjtc avatar Oct 10 '24 23:10 bryanjtc

Same here. Downgrade to [email protected] helped, thanks @typesafeui . Hopefully someone fixes it

jiri-jirsa avatar Oct 12 '24 10:10 jiri-jirsa

I also found that importing this way works in "next": "14.2.16":

'use client'

import {Document, Page, View, Text} from "@react-pdf/renderer/lib/react-pdf.browser";

romanslonov avatar Oct 29 '24 10:10 romanslonov

If you are using next js you need use client. Server side PDF generation is not supported

diegomura avatar Nov 16 '24 23:11 diegomura

I also found that importing this way works in "next": "14.2.16":

'use client'

import {Document, Page, View, Text} from "@react-pdf/renderer/lib/react-pdf.browser";

Thanks, this worked for me, and it was easier than downgrading to [email protected], as that version was not compatible with the lastest version of next-intl

brianonchain avatar Nov 17 '24 07:11 brianonchain

If anyone is having this issue, there are two temporary fixes:

* Downgrade to `[email protected]` and to make it work for now

* Put "use client" in the top of the page.tsx file if possible ( not possible if you have any server logic in the page.tsx file )

These are just patches to make it work for now though

Hello,

Thanks, it works locally but do you manage to make it run in production in a docker ? It does not build for me.

Have a good day

cor3ntino avatar Dec 02 '24 13:12 cor3ntino

Same here

"@react-pdf/renderer": "^4.1.5",
"next": "14.2.15",

MrRedu avatar Dec 02 '24 18:12 MrRedu

Same here

"@react-pdf/renderer": "^4.1.5",
"next": "14.2.15",

I managed to make it work both locally and on docker; you need:

"@react-pdf/renderer": "^4.1.5"
"next": "14.2.8", 

next version is very important, other versions of next had an issue on my end

Also, i needed to remove both the JavaScript polyfills and the PDF.js worker; so REMOVE stuff like that from the file where you implemented your renderer component:

// Core JavaScript polyfills
if (typeof Promise.withResolvers === "undefined") {
  // @ts-expect-error This does not exist outside of polyfill which this is doing
  Promise.withResolvers = function () {
    let resolve, reject;
    const promise = new Promise((res, rej) => {
      resolve = res;
      reject = rej;
    });
    return { promise, resolve, reject };
  };
}

// PDF.js specific configuration
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
  "pdfjs-dist/legacy/build/pdf.worker.min.mjs",
  import.meta.url
).toString();

Also, i am on node:23.3.0-alpine on docker but i feel like other versions work too.

cor3ntino avatar Dec 02 '24 20:12 cor3ntino

If you are using next js you need use client. Server side PDF generation is not supported

Could you elaborate on this? I thought you could generate PDF documents both on the client and server?

I currently have a NextJS API route that is generating a PDF document and using renderToBuffer and returning it to be downloaded on the client.

I've also got a preview document feature on the UI that uses shared report components used in the document generated via the API route and that is where I'm encountering this error.

kyle-robinson-morpheus avatar Dec 11 '24 18:12 kyle-robinson-morpheus

I also found that importing this way works in "next": "14.2.16":

'use client'

import {Document, Page, View, Text} from "@react-pdf/renderer/lib/react-pdf.browser";

+1 This worked for me too.

TeraBiusT2 avatar Jan 08 '25 20:01 TeraBiusT2

Had that on:

"next": "^14.2.23",
"@react-pdf/renderer": "^4.1.6",

As others mentioned this worked for the client-side rendering

'use client'

import { Document, Page, View, Text, Font } from "@react-pdf/renderer/lib/react-pdf.browser";

But I could still manage to server-side render PDFs with

import { Font, renderToBuffer } from '@react-pdf/renderer'

const pdfBuffer = await renderToBuffer(<InvoicePDFTemplate invoices={invoices} />)
...
// do something with the buffer

where my InvoicePDFTemplate is using

import { Document, Font, Image, Page, StyleSheet, Text, View } from "@react-pdf/renderer/lib/react-pdf.browser"

so I was able to reuse the component both on client and server.

nyxz avatar Jan 30 '25 23:01 nyxz

Neither of

* Downgrade to `[email protected]` and to make it work for now

* Put "use client" in the top of the page.tsx file if possible ( not possible if you have any server logic in the page.tsx file )

worked for us.

We have a server generated page, that is updated in the frontend as needed.

Dynamically importing the modals that used the library did the trick.

matthewhoth avatar Feb 04 '25 15:02 matthewhoth

I was on "next": "14.2.13" and had to bump to "next": "14.2.23" to resolve the issue along with using @react-pdf/renderer/lib/react-pdf.browser for any imports. Seemed like a safer option rather than downgrading.

Sicria avatar Feb 28 '25 02:02 Sicria

I was on "next": "14.2.13" and had to bump to "next": "14.2.23" to resolve the issue along with using @react-pdf/renderer/lib/react-pdf.browser for any imports. Seemed like a safer option rather than downgrading.

Thanks to this., I resolved the issue by adding "/lib/react-pdf.browser" to my import

bustilloroy avatar Mar 06 '25 02:03 bustilloroy

The error is still happening, i had to move the pdf generation to the server side with renderToBuffer so that i could solve this. The react-pdf.browser import didn't work for me and i didn't want to risk downgrading the next version just for that, seemed too risky.

"@react-pdf/renderer": "^4.3.1", "next": "14.2.32",

leonardo-weber avatar Oct 27 '25 19:10 leonardo-weber

I encountered this issue while running jest unit tests instead. Mocking Document and Page was able to solve it for me:

jest.mock(‘react-pdf’, () => ({
  pdfjs: {
    GlobalWorkerOptions: {
      workerSrc: ''
    }
  },
  Document: jest.fn(),
  Page: jest.fn()
}))

I am on react-pdf v9.

ryanmlow avatar Nov 10 '25 09:11 ryanmlow