Unlocking the Power of JSX Components: Viewing and Downloading PDFs in React

Yug Jadvani
JavaScript in Plain English
3 min readMay 1, 2024

--

Photo by Rahul Mishra on Unsplash

In the realm of modern web development, ReactJS stands tall as a powerhouse for building dynamic and interactive user interfaces. Yet, its capabilities extend beyond mere presentation layers. With the integration of JSX components, React empowers developers to seamlessly generate and manipulate PDF documents, enriching user experiences with printable content. In this guide, we’ll embark on a journey through the integration of JSX components to effortlessly view and download PDFs within ReactJS applications.

1. Installation:

First things first, let’s set the stage by installing the necessary dependencies. With React-pdf, the process is as smooth as a breeze:

npm install @react-pdf/renderer --save

Or if you prefer Yarn:

yarn add @react-pdf/renderer

Ensuring the presence of React and react-dom alongside react-pdf is vital for seamless integration. If you haven’t already, ensure React is part of your project setup.

2. Crafting Your PDF Document:

Now, let’s delve into the creative process of crafting your PDF masterpiece. React-pdf equips developers with a set of intuitive React primitives, simplifying the rendering of content within PDF documents. Let’s bring your PDF to life with a glimpse into the code:

import React from 'react';
import { Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';

// Define styles
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4'
},
section: {
margin: 10,
padding: 10,
flexGrow: 1
}
});

// Create Document Component
const MyDocument = () => (
<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>
);

Voila! With this snippet, you’ve birthed a PDF document with a single page, adorned with two distinct sections of text. The possibilities extend far beyond; feel free to explore additional primitives and examples for further enrichment.

3. Rendering Your Document:

With your PDF document meticulously crafted, it’s time to decide its stage for presentation. React-pdf graciously offers two avenues for rendering: web and server environments. The process remains akin, tailored to the unique needs of each domain.

Rendering in the DOM:

Incorporating your PDF into a web environment is a seamless affair. Utilizing the PDFViewer component, the document elegantly finds its place within your React application:

import React from 'react';
import ReactDOM from 'react-dom';
import { PDFViewer } from '@react-pdf/renderer';

const App = () => (
<PDFViewer>
<MyDocument />
</PDFViewer>
);

4. Downloading Your PDF:

The cherry on top of the PDF-generating cake is the ability to offer users the option to download their newfound treasure. Enter the PDFDownloadLink component, delivering a delightful user experience:

import React from 'react';
import { PDFDownloadLink } from '@react-pdf/renderer';

const DownloadButton = () => (
<PDFDownloadLink document={<MyDocument />} fileName="PDFRenderer.pdf">
{({ blob, url, loading, error }) => (loading ? "Loading document..." : "Download PDF")}
</PDFDownloadLink>
);

In Conclusion:

With the fusion of JSX components and React PDF, the landscape of PDF generation within ReactJS applications is transformed into a realm of boundless creativity. Whether it’s crafting reports, generating invoices, or compiling documentation, the power rests in your hands to deliver polished and printable content seamlessly.

A Heartfelt Thanks:

As we draw the curtains on this journey, I extend my sincerest gratitude to you, dear reader, for accompanying us through the corridors of JSX component magic. May your endeavors in ReactJS continue to flourish, adorned with the elegance of PDF generation. Until we meet again on the pathways of knowledge, happy coding!

Enjoyed the read? If you found this article insightful or helpful, consider supporting my work by buying me a coffee. Your contribution helps fuel more content like this. Click here to treat me to a virtual coffee. Cheers!

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--

Front-End Developer, design / develop⚡️web and mobile applications by using web technologies such as HTML, CSS, JavaScript, ReactJs, NextJs, and React Native