Generating a PDF from a webpage is becoming essential in web development, especially in a report-driven application. It is often used in scenarios ranging from creating an invoice in an e-commerce application, printing transactional reports, generating reports, etc. In this article, we will explore how to create an application that generates, prints, downloads, and shares a PDF with
ReactJS
and
React-PDF
.
You can also check out
eduproject
, explaining the design and implementation of custom PDF using React and React-pdf.
Together, we will go through step by step procedure I use to design an e-commerce invoice with the
react-pdf
package.
Next, let’s create these files
Invoice.js
and
PdfCard.js
, we will be adding our code to them as we go along in this guide.
If using a Mac OS, let’s run the below command to create these files
touch src/Invoice.js touch src/PdfCard.js
If using a Windows OS, let’s run the below command to create the files, make sure to run the command one after the other.
cd.> src/Invoice.js
Basically, we will be working with the
App.js
file which is our root file, the
Invoice.js,
and
PdfCard.js
.
The
react-pdf
package will be used to design an e-commerce invoice sample. Before diving in properly, let’s work through the building block of the
react-pdf
package.
PDFViewer
: This component will display the PDF documents within the web browser.
Document
: This component represents the PDF document and is the parent component to other components.
Page
: As the name implies, it represents a single page within a PDF document.
View
: This is the core component for designing the UI.
Text
: This component is used for displaying text
Image
: This component displays images within the document. Support image formats such as JPG, PNG, GIF, etc.
PDFDownloadLink
: This component creates a download link for a PDF document generated by the React-pdf library.
BlobProvider
: This component generates a PDF document and provides it as a Blob object that can be used to display or download the PDF document.
Now we have a PDF Invoice file that can be downloaded, printed, and shared via email.
In a real-world scenario, it is likely we have an Invoice or list of invoices requiring actions such as downloading, printing, and sharing via email. Using some custom hooks from React-PDF, we can implement these features. Let's update our design to accommodate these action buttons (download, print, and share).
Let's create a new file PdfCard.js update as below:
To download PDF we will be using a custom hook PDFDownloadLink from React-PDF. This component creates a download link for a PDF document generated by the react-pdf library.
Let's update our PdfCard.js with the PDFDownloadLink as below:
To print PDFs, we will use a custom hook BlobProvider from React-PDF. This component generates a PDF document and provides it as a Blob object that can be used to display or download the PDF document.
Let's update our PdfCard.js with the BlobProvider as below:
To share PDFs via email, we will use a custom hook, BlobProvider from React-PDF. This implementation will download a PDF file to the computer's local disk and open a mailbox for us to attach the downloaded file.
To see this in action, let's update our PdfCard.js as below: