Skip to content

Instantly share code, notes, and snippets.

@wp126
Created June 24, 2024 05:37
Show Gist options
  • Save wp126/bfbc98ae41ad83d2b662308821e6a29b to your computer and use it in GitHub Desktop.
Save wp126/bfbc98ae41ad83d2b662308821e6a29b to your computer and use it in GitHub Desktop.
Download Pdf In React On page load
// src/App.js
import React from 'react';
import { pdf, Document, Page, Text,StyleSheet,View } from '@react-pdf/renderer';
import { saveAs } from 'file-saver';
function App() {
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4',
},
section: {
margin: 10,
padding: 10,
flexGrow: 1,
},
});
const handleDownload = async () => {
const doc = (
<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>
);
try {
const blob = await pdf(doc).toBlob();
saveAs(blob, 'minimalDocumdent.pdf');
} catch (error) {
console.error('Error downloading PDF:', error);
}
};
handleDownload();
return (
<div className="App">
<h1>Download Minimal PDF Document</h1>
<button onClick={handleDownload}>Download PDF</button>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment