React PDF" can refer to two main functionalities within
the React ecosystem:
Code
import { Document,
Page } from 'react-pdf';
import { pdfjs } from 'react-pdf';
// Set up the worker source for
react-pdf
pdfjs.GlobalWorkerOptions.workerSrc =
`//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
function MyPdfViewer() {
const pdfFile =
"your-document.pdf"; // Can be a URL, base64 string, etc.
return (
<Document file={pdfFile}>
<Page pageNumber={1} />
{/* Render the first page */}
</Document>
);
}
Code
import React from
'react';
import { Document, Page, Text, View,
StyleSheet, pdf } 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>Hello from React
PDF!</Text>
</View>
</Page>
</Document>
);
// To generate and download the PDF
in the browser:
// pdf(<MyDocument
/>).toBlob().then(blob => {
//
const url = URL.createObjectURL(blob);
//
const a = document.createElement('a');
//
a.href = url;
//
a.download = 'my-document.pdf';
//
a.click();
// });