【问题标题】:Error "render is a Node specific API" when using the module @react-pdf/renderer使用模块 @react-pdf/renderer 时出现错误“render is a Node specific API”
【发布时间】:2021-04-20 20:29:48
【问题描述】:

使用模块@react-pdf/renderer与官方示例(https://snyk.io/advisor/npm-package/@react-pdf/renderer):

    import ReactPDF, { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';

    // Create styles
    const styles = StyleSheet.create({
        page: {
        flexDirection: 'row',
        backgroundColor: '#E4E4E4'
        },
        section: {
        margin: 10,
        padding: 10,
        flexGrow: 1
        }
    });
    
    // Create Document Component
    const MyDocument = (): JSX.Element => (
        <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>
    );

    ReactPDF.render(<MyDocument />, `example.pdf`);

但我得到一个错误:

错误:render 是特定于节点的 API。你要么使用这种方法 在浏览器中,或者您的捆绑程序没有从 适当的网络构建。

请告诉我为什么会发生此错误以及如何解决它

【问题讨论】:

  • 我在这里遇到了同样的问题!

标签: reactjs typescript pdf pdf-generation react-pdf


【解决方案1】:

render 函数只在服务端工作,react 在客户端工作,如果你想在浏览器中渲染,请检查 ReactDOM.render 函数是否使用了 PDFViewer 组件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2015-10-16
    • 2022-11-30
    • 2021-10-28
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多