Generate invoice with React-js

Generate invoice with react-js

Are you new to react and you want to create something new with it?! Then this article is for you. Here we are going to create a billing application which could just convert our HTML form into a PDF file which will look like a copy of bill. Let’s get started!


About our app:

Our app will have the following qualities:

  • We could add item name, quantity, cost of one item and their corresponding total which we will calculate based on the number of quantity and cost we add in the input box.
  • Also we are going to calculate total billing amount based on total of each items added.
  • Upon clicking submit button

Creating state named item:

Let’s create a state named item and then initialize the needed objects value with empty or zero as below,

We have added item name as iname, quantity, amount of one item as amount, then total for each item based on quantity and amount we add.

Creating front-end UI part:

Now, let us create a html form inside App.js file of react app and add all the necessary input fields inside of it like below,

In the above code, we are making use of item as array and trying to create that many input fields based on that item count.

Adding more input box upon clicking add button:

Now let’s add some more features to our static website. Let’s add onClick functionality which could add additional textbox upon clicking the add button. Also lets add addItem function which could change state of item with setItem function.

Handling change in input box:

Whenever we change input box values, we need to update it to our state value named ‘item’ for calculating the total of each item added in the form. Let’s create a function handleChange and then change those accordingly,

Adding functionality to remove button:

Now it’s time to add function to remove button. Upon clicking remove button, we should pass the index value of corresponding input field and then remove that index value from item. We could do that with below piece of code,

Calculating total billing amount:

We are almost near to the creation of billing app. It’s time to calculate total billing amount based on our inputs which we gave in each input fields. Let’s add a total billing amount as new state and also add it inside our form tag which could change dynamically,

Inside form:

Modifying billing amount with react useEffect:

Converting html form into PDF:

Now we have come to end of our coding part. We need to convert our form values whichever we added into a PDF file. Lets import the jspdf and jspdf-autotable and then add handleSubmit function to the App.js file.

That’s it!! We are done with the conversion of HTML form into PDF with React. Your final App.js will look like below,

My completed react project can be found in following git repository — link

I will add one more functionality of saving it in database and do another blog about it.

Happy coding :-)



Programmer who loves to do things creatively. #automationTester by profession #javascript #nodejs #reactjs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Siddharth Murugan

Programmer who loves to do things creatively. #automationTester by profession #javascript #nodejs #reactjs