【问题标题】:Error: stream.push() after EOF in react-pdf错误:react-pdf 中 EOF 之后的 stream.push()
【发布时间】:2019-10-08 18:02:55
【问题描述】:

我正在尝试使用 react-pdf 打开一个 pdf 文档

const PDFDocument = () => {

  const [PDFData, setPDFData] = useState(() => {
    const data = localStorage.getItem('pdfData')
    return JSON.parse(data)
  })

  useEffect(() => {
    console.log(PDFData)
  }, [PDFData])

  const { NameOfService, AccessibilityDetails, AddressLine1, AddressLine2, AddressLine3, Postcode, OtherDetailedInformation, MondayStart, MondayEnd, TuesdayStart, TuesdayEnd, WednesdayStart, WednesdayEnd, ThursdayStart, ThursdayEnd, FridayStart, FridayEnd, SaturdayStart, SaturdayEnd, SundayStart, SundayEnd, Cost,  Buses, TubeAndTrains, CarParkingDetails, Name1, PhoneNumber1, Email1, Name2, PhoneNumber2, Email2, Website, OtherContactInfo  } = PDFData

  return (
    <PDFViewer>
      <Document>
        <Page size="A4" style={styles.page}>
          <View style={styles.heading}>
            <Text>{NameOfService}</Text>
          </View>
          <View style={styles.text}>
            <Text>{AddressLine1}</Text>
            <Text>{AddressLine2}</Text>
            <Text>{AddressLine3}</Text>
            <Text>{Postcode}</Text>
          </View>
          <View style={styles.text}>
            <Text>{OtherDetailedInformation}</Text>
          </View>
          <View style={styles.text}>
            {MondayStart ? <Text>Monday: {MondayStart} - {MondayEnd}</Text> : null}
            {TuesdayStart ? <Text>Tuesday: {TuesdayStart} - {TuesdayEnd}</Text> : null}
            {WednesdayStart ? <Text>Wednesday: {WednesdayStart} - {WednesdayEnd}</Text> : null}
            {ThursdayStart ? <Text>Thursday: {ThursdayStart} - {ThursdayEnd}</Text> : null}
            {FridayStart ? <Text>Friday: {FridayStart} - {FridayEnd}</Text> : null}
            {SaturdayStart ? <Text>Saturday: {SaturdayStart} - {SaturdayEnd}</Text> : null}
            {SundayStart ? <Text>Sunday: {SundayStart} - {SundayEnd}</Text> : null}
          </View>
          <View style={styles.text}>
            <Text>Cost: {Cost}</Text>
          </View>
          <View style={styles.text}>
            <Text>Transport:</Text>
            <Text>Buses: {Buses}</Text>
            <Text>Tube and Trains: {TubeAndTrains}</Text>
            <Text>Car park Availibility: {CarParkingDetails}</Text>
          </View>
          <View style={styles.text}>
            {AccessibilityDetails ? <Text>Accessibility: {AccessibilityDetails}</Text> : null}
          </View>
          <View style={styles.text}>
            <Text>Contact Details:</Text>
            <Text>{Name1}: {PhoneNumber1}; {Email1}</Text>
            {Name2 ? <Text>{Name2}: {PhoneNumber2}; {Email2}</Text> : null}
          </View>
          <View style={styles.text}>
            {Website ? <Text>{Website}</Text> : null}
          </View>
          <View style={styles.text}>
            {OtherContactInfo ? <Text>{OtherContactInfo}</Text> : null}
          </View>
        </Page>
      </Document>
    </PDFViewer>
  )
};

export default PDFDocument;

当有人点击“打印”按钮时,本地存储数据会在 App 组件中设置。

handlePrint = (pdfData) => {
    localStorage.setItem('pdfData', JSON.stringify(pdfData))
  }

使用 react-router 在新选项卡中打开 pdf

<Route path="/pdf" render={ () => <PDFDocument/> } />

选项卡打开时显示 pdf 但没有数据,有两个页面,并且控制台中出现错误stream.push() after EOF in react-pdf

Live site pdf 无法打开。

【问题讨论】:

    标签: reactjs pdf react-pdf


    【解决方案1】:

    我今天刚刚开始工作,但遇到了相同的 EOF 错误和两个空白页。我认为问题在于 PDF 渲染器或文档尚未完全加载/生成,因此您需要先调用一个函数来实际生成 pdf,然后在该函数完成后显示 PDF 查看器以查看或获取此文档是我所做的修复:

    import React from 'react';
    import { Button } from 'react-bootstrap';
    import { Text, Image, View, Page, Document, PDFDownloadLink } from '@react-pdf/renderer';
    
    import './App.css';
    
    export default class App extends React.Component {
        constructor() {
            super();
    
            this.state = {
                ready: false
            }
        }
    
        toggle() {
            this.setState((prevState) => ({
                ready: false
            }), () => {     // THIS IS THE HACK
                setTimeout(() => {
                    this.setState({ ready: true });
                }, 1);
            });
        }
    
        render() {
            const { ready } = this.state;
    
            const MyDocument = (
                <Document>
                    <Page size="A4">
                        <View>
                            <Text>
                                some text
                            </Text>
                        </View>
                    </Page>
                </Document>
            );
    
            return (
                <div>
                    {ready && (
                        <PDFDownloadLink document={MyDocument} fileName="PDF">
                            {
                                ({ blob, url, loading, error }) => (loading ? 'Loading document...' :
                                    <Button onClick={() => (this.setState({ ready: false }))}>
                                        download pdf
                                    </Button>
                                )
                            }
                        </PDFDownloadLink>
                      )}
                    {!ready && (
                       <Button onClick={() => this.toggle()}>
                            generate pdf
                        </Button>
                    )}
                </div>
            );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-16
      • 2013-08-25
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2015-05-18
      • 2014-10-17
      • 2016-12-12
      相关资源
      最近更新 更多