While working on web/mobile applications, It is a very common requirement to generate pdf to share forms, invoices with the user. There are various packages available in different languages for achieving this task. If we talk in Angular or React jsPDF is very commonly used for this task. It has got more than 18k Github stars and gets downloaded 1,52,000 times weekly.
In this article, We will how to generate simple PDF file in React application using jsPDF package.
Create a basic application entering below command over the terminal.
npx create-react-app react-pdf-app
#for NPM
npm install --save jspdf
#for yarn
yarn add jspdf
Next task is to import the above-installed package into app component. So open App.js file and add below code on top.
import jsPDF from 'jspdf'
jsPDF examples
In the below code we will see how to create jsPDF function object and how to use different methods to perform the task.
doc.text() is used to set the text with the position. The first two arguments are the position coordinates X,Y for the text.
doc.setFont() is used to set the specific font for the text
doc.addPage() is used above to create a new page in the pdf.
doc.save() takes the name of the pdf file as an argument
Finally, now run the app on the terminal using command npm start and check the functionality on browser.
In this article, We learn how to generate PDF file in React application with.
I hope this article helped you. You can also find other demos at React.js Sample Projects here to start working on enterprise-level applications.
Let me know your thoughts over email pankaj.itdeveloper@gmail.com. I would love to hear them and If you like this article, share it with your friends.
Thank You!