Creating or rendering a PDF file in a web page is a common requirement. It allows user to download the pdf file and view it offline in some scenarios. Today, we will learn how to render and generate a PDF file from React using the react-pdf library.
The react-pdf library is a React component that allows us to design and render PDF documents in React. Let’s get started an example, it’s very simple to create a pdf and render it in React.
Install the react-pdf Library
1
npm install @react-pdf/renderer --save
Create a PDF Document
We will create a simple PDF document using the react-pdf library. It supports customize styles and Flex layout to create the PDF. Below code example using react-pdf library to create a PDF document, including using Document, Page, StyleSheet, Image, View, Text, Link components.
constPdfDocument = () => { return ( <Documenttitle="My PDF Document"author="Andrewsy"subject="This is a sample PDF document"> <Pagesize="A4"style={styles.page}> <Viewstyle={styles.section}> <Textstyle={styles.header}>React PDF Renderer</Text> <Textstyle={styles.text}>The react-pdf library allows you to render PDF documents using React components.</Text> <Imagestyle={styles.logo}src="images/logo.png"/> </View> <Viewstyle={styles.section}> <Textstyle={styles.title}>Install react-pdf library</Text> <Textstyle={styles.text}>To install the react-pdf library, you can use npm or yarn:</Text> <Textstyle={styles.code}>npm install @react-pdf/renderer</Text> <Text></Text> <Textstyle={styles.code}>yarn add @react-pdf/renderer</Text> <Textstyle={styles.text}>For component documentation, visit the official website <Linksrc="https://react-pdf.org/">@react-pdf/renderer</Link>.</Text> </View> </Page> </Document> ); };
exportdefaultPdfDocument;
From above, we can see that we have created a PDF document using react-pdf library. We have used Document and Page components to create the PDF document. We have also used StyleSheet to define the styles for the PDF document. And using Image, View, Text, Link components to create the content of the PDF document.
View PDF Document
To view the PDF document, we need to use PDFViewer component to render the PDF document in the browser. There is one thing need notice, if your project is using Next.js, and you want to use PDFViewer or PDFDownloadLink component in client side render directly, you will get below error:
1
[Error: PDFViewer is a web specific API. You're either using this component on Node, or your bundler is not loading react-pdf from the appropriate web build.]
To fixed the issue in Next.js project. Use Next dynamic function to manually set server-side rendering off and import it through that function instead of the regular import
The view PDF document code is below, for the PDFViewer component, we need to set the width and height of the PDF document. Actually it is rendered as a iframe element in the browser. This width and height is setting to iframe.
In the above code, we have used PDFDownloadLink component to download the PDF document, passed the PdfDocument component as the document prop of PDFDownloadLink component. We have also set the fileName prop to my-pdf-document.pdf to set the file name of the downloaded file. The style prop is used to set the style of the download button. We also use the loading prop to show the loading message while the PDF document is being downloaded.
React PDF Download
Below PDF document is downloaded as a file.
React PDF Document Opened in Adobe Acrobat Reader
See it’s easily to create and render a PDF document in React using the react-pdf library.
Conclusion
The react-pdf library is a React component that allows us to design and render PDF documents in React. It supports customize styles and Flex layout to create the PDF. We have learned how to create a PDF document using react-pdf library, how to view and download the PDF document.