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

V4 Broken renderToStream

Open cutefatboy opened this issue 2 years ago • 4 comments

Describe the bug Error Here:

Error = "Error generating PDF: Error: Minified React error #31; visit https://reactjs.org/docs/error-decoder.html?invariant=31&args[]=object%20with%20keys%20%7B%24%24typeof%2C%20type%2C%20key%2C%20props%2C%20_owner%2C%20_store%7D for the full message or use the non-minified dev environment for full errors and additional helpful warnings."

"  29 |
  30 |   const updateContainer = (doc, callback) => {
> 31 |     renderer.updateContainer(doc, mountNode, null, callback);
     |             ^
  32 |   };
  33 |"

To Reproduce

Code Preview for [resume/[_id]/pdf] route.tsx

import { getResumeByIDFromDB } from "@/actions/resume";
import { NextResponse } from "next/server";
import { Page, Text, View, Document, StyleSheet, renderToStream } from '@react-pdf/renderer';

const styles = StyleSheet.create({
    page: {
        flexDirection: 'row',
        backgroundColor: '#E4E4E4'
    },
    section: {
        margin: 10,
        padding: 10,
        flexGrow: 1
    }
});


const MyDocument = () => (
    <Document>
        <Page size="A4" style={styles.page}>
            <View style={styles.section}>
                <Text>Section #1</Text>
            </View>
            <View style={styles.section}>
                <Text>Section #2</Text>
            </View>
        </Page>
    </Document>
);


export async function GET(
    request: Request,
    { params }: { params: { _id: string } }
) {
    try {
        const id = (await params)._id;
        const resume = await getResumeByIDFromDB(id);
        console.log("Param ID: ", id) //Param ID is correct and logs correctly.

        if (!resume) {
            return new NextResponse('Resume not found', { status: 404 });
        }

        const stream = await renderToStream(<MyDocument />); //Code dies here.
        console.log("Stream: ", stream); //Can't even get to here.

        const headers = new Headers({
            'Content-Type': 'application/pdf',
            'Content-Disposition': 'inline',
        });

        return new NextResponse(stream as unknown as ReadableStream, { headers });
    } catch (error) {
        console.error('Error generating PDF:', error);
        return new NextResponse('Error generating PDF', { status: 500 });
    }
}

Expected behavior Should be able to render a PDF with text.

Desktop (please complete the following information):

  • MacOS
  • React-pdf version [e.g. v4]

cutefatboy avatar Nov 15 '24 21:11 cutefatboy

Im an idiot and was on NextJS 15

cutefatboy avatar Nov 16 '24 04:11 cutefatboy

Im an idiot and was on NextJS 15

Hi, Have you solved the problem?

yannloosli avatar Nov 16 '24 18:11 yannloosli

I have the same problem on NextJS 15

JoeKyx avatar Nov 17 '24 16:11 JoeKyx

I didn't manage to fix it with code similar to the one above.

My app already had a context so I followed the example here for 14 (since 15 didn't work...)

https://github.com/diegomura/react-pdf/tree/master/packages/examples/next-14/app

I created the example of the document, passed in my data.context, and it worked.

cutefatboy avatar Nov 17 '24 21:11 cutefatboy