gpt4 book ai didi

reactjs - react-pdf中EOF后出现错误: stream. push()

转载 作者:行者123 更新时间:2023-12-03 09:36:55 25 4
gpt4 key购买 nike

我正在尝试使用 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))
}

pdf 使用 react-router 在新选项卡中打开
<Route path="/pdf" render={ () => <PDFDocument/> } />

该选项卡以 pdf 格式打开,但没有数据、两页以及控制台中的错误 stream.push() after EOF in react-pdf
Live site pdf 打不开。

最佳答案

我今天才开始使用它,但我遇到了相同的 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>
);
}
}

关于reactjs - react-pdf中EOF后出现错误: stream. push(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58291789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com