I want to generate a pdf from the UI and download it. Been looking to the documentation but couldn't find how to implement I.e. onClick={this.downloadPdf}
here's the module reference: https://github.com/diegomura/react-pdf
It says: Save in a file
import React from 'react';
import ReactPDF from '@react-pdf/react-pdf';
ReactPDF.render(<MyDocument />, `${__dirname}/example.pdf`);
But this doesn't make much sense to me.
I have found below in their site documentation
import { PDFDownloadLink, Document, Page } from '@react-pdf/renderer'
const MyDoc = () => (
<Document>
<Page>
// My document data
</Page>
</Document>
)
const App = () => (
<div>
<PDFDownloadLink document={<MyDoc />} fileName="somename.pdf">
{({ blob, url, loading, error }) => (loading ? 'Loading document...' : 'Download now!')}
</PDFDownloadLink>
</div>
)
hi its working fine for me
import React from 'react';
import logo from './logo.svg';
import './App.css';
import { PDFDownloadLink, Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4'
},
section: {
margin: 10,
padding: 10,
flexGrow: 1
}
});
const MyDoc = () => (
<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>
);
function App() {
return (
<div className="App">
<PDFDownloadLink document={<MyDoc />} fileName="somename.pdf">
{({ blob, url, loading, error }) => (loading ? 'Loading document...' : 'Download now!')}
</PDFDownloadLink>
</div>
);
}
export default App;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With