AskHandle Blog
How to Dynamically Generate PDFs in React?

How to Dynamically Generate PDFs in React?
Are you looking to create dynamic PDF generation functionality in your React application? Generating PDFs on the fly can enhance various aspects of your application, such as generating reports, exporting data, or creating invoices.
This article explains how to achieve dynamic PDF generation in React using a popular library called React-PDF. React-PDF is a versatile library that provides tools to create PDF documents in your React application.
Setting Up React-PDF
To start, set up your React project and install the necessary dependencies. If you have a React project, navigate to your project directory and install React-PDF using npm or yarn:
1npm install @react-pdf/rendererOnce React-PDF is installed, you are ready to build your dynamic PDF generation functionality.
Creating a Basic PDF Document
To create a basic PDF document using React-PDF, define the structure and layout of your PDF. React-PDF follows a component-based approach, making it easy to use.
Below is an example of a simple PDF document that contains a title and some text:
1import React from 'react';
2import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';
3
4const styles = StyleSheet.create({
5 page: {
6 flexDirection: 'row',
7 backgroundColor: '#ffffff',
8 },
9 section: {
10 margin: 10,
11 padding: 10,
12 },
13});
14
15const MyDocument = () => (
16 <Document>
17 <Page size="A4" style={styles.page}>
18 <View style={styles.section}>
19 <Text>This is a dynamically generated PDF using React-PDF.</Text>
20 </View>
21 </Page>
22 </Document>
23);
24
25export default MyDocument;In this example, we define a simple PDF document using the Document, Page, Text, and View components from React-PDF. You can style your PDF document using the StyleSheet object.
Adding Dynamic Data
Including dynamic data in PDFs is a key feature. React-PDF allows you to pass data to your PDF components like any other React component.
Let's modify the previous example to include dynamic data passed as props:
1import React from 'react';
2import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';
3
4const styles = StyleSheet.create({
5 page: {
6 flexDirection: 'row',
7 backgroundColor: '#ffffff',
8 },
9 section: {
10 margin: 10,
11 padding: 10,
12 },
13});
14
15const MyDocument = ({ title, content }) => (
16 <Document>
17 <Page size="A4" style={styles.page}>
18 <View style={styles.section}>
19 <Text>{title}</Text>
20 <Text>{content}</Text>
21 </View>
22 </Page>
23 </Document>
24);
25
26export default MyDocument;Now, you can pass dynamic data to your PDF component when rendering it in your React application:
1import React from 'react';
2import { PDFViewer } from '@react-pdf/renderer';
3import MyDocument from './MyDocument';
4
5const App = () => (
6 <PDFViewer>
7 <MyDocument
8 title="Dynamic PDF Generation"
9 content="This PDF is dynamically generated with React-PDF."
10 />
11 </PDFViewer>
12);
13
14export default App;Styling Your PDF
Styling is important for making your PDF visually appealing and readable. React-PDF provides a StyleSheet object to define styles for your PDF components.
Customize fonts, colors, margins, padding, and more using the StyleSheet object. Here’s how you can style your PDF document with React-PDF:
1const styles = StyleSheet.create({
2 page: {
3 flexDirection: 'row',
4 backgroundColor: '#ffffff',
5 },
6 section: {
7 margin: 10,
8 padding: 10,
9 flexGrow: 1,
10 },
11 title: {
12 fontSize: 24,
13 marginBottom: 10,
14 textAlign: 'center',
15 },
16 content: {
17 fontSize: 16,
18 textAlign: 'left',
19 },
20});Apply these styles to your PDF components to achieve the desired layout.
Generating PDFs on User Interaction
You may want to generate PDFs based on user interactions, such as button clicks. React-PDF makes it easy to generate PDFs dynamically in response to user actions.
Let’s create a simple PDF generator component that generates a PDF on a button click:
1import React from 'react';
2import { PDFDownloadLink } from '@react-pdf/renderer';
3import MyDocument from './MyDocument';
4
5const PDFGenerator = () => (
6 <div>
7 <PDFDownloadLink
8 document={<MyDocument title="Generated PDF" content="This PDF is dynamically generated on button click." />}
9 fileName="generated_pdf.pdf"
10 >
11 {({ blob, url, loading, error }) => (loading ? 'Generating PDF...' : 'Download PDF')}
12 </PDFDownloadLink>
13 </div>
14);
15
16export default PDFGenerator;In this example, when the user clicks the download link, the PDF is generated dynamically and available for download. Customize the filename and content based on your needs.
This article showed how to dynamically generate PDFs in React using the React-PDF library. Follow these steps to implement PDF generation functionality in your React applications.