我正在尝试使用 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
无法打开 pdf 的实时站点。