posted 5 years ago
With the continuous development of e-commerce, electronic invoices are adopted by more and more businesses, among which PDF invoices are one of the most popular ones. In this article, I’ll share with you a free and brilliant solution of creating PDF invoices in Java.
A typical invoice contains the names and addresses of customer and supplier, the invoice number, the descriptions of items purchased, the payment amount, etc. These information varies from one document to another. To generate invoices dynamically, I created a template with MS Word, in which it is easy to have your desired layout pre-defined. Then I modified the template document and saved it as a PDF file using Free Spire.Doc for Java 2.0.0.
Create a Template in Word
As shown in the figure 1, the Word invoice template consists of three tables. Table one and table two display the seller information and buyer information. Table three specify the products or services the seller had provided the buyer. What we need to do is replace the text in the tables that starts with # and fill the third table with the customer’s purchase list.
In order to calculate the total amount automatically, some formulas have been added to the particular cells. For example, the cell D2 contains a formula of “=B2*C2”, which calculates the total price of the item that will be written in the cell A2. While we add more items (rows) to the table, we need to update the formulas of some cells dynamically.
Figure 1. Invoice Template
How to Replace Text in Word
Spire.Doc has a method IBodyRegion.replace(java.lang.String given, java.lang.String replace, boolean caseSensitive, boolean wholeWord) that we can use to replace a certain string in the document with a new string. For instance, to replace “#InvoiceNum” with “17854”, use the following line of code, where doc is a Document object.
How to Update an Existing Table in Word
Now, let’s see how to add rows to the existing table, how to fill the table with data and how to update formulas dynamically. To make the logic clearer, I customized three functons and made the below graph (Figure 2) to demonstrate how they function and the invoke relationship between them.
Figure 2. Custom Functions for Updating Table Three
Here come the code snippets and detailed instuctions for these three custom functions.
The writeDataToDocument() method takes a parameter of an String[][] object, which stores one customer’s purchase information. Each element of it is a String array that can be set up like this:
The length of the String[][] is acutally the number of the items a customer purchased. If the length is greater than one, we’ll need to add rows at the number of length – 1.
Generating Invoice
Here's the code for generating PDF invoices in the main method.
The result document is showing below.
Figure 3. Invoice for Multiple Items
If you have the purchase data like this, then you’ll get the output shown in Figure 4.
Figure 4. Invoice for One Item
P.S. Free Spire.Doc for Java is capable of loading a Word document having no more than 500 paragraphs or 25 tables, and converting the first 3 pages into PDF. Most PDF invoices have only one or two pages. So, feel free to enjoy this solution, it is completely free.