0% found this document useful (0 votes)
70 views

How To Generate PDF Invoice Using Java - CodeSpeedy

Uploaded by

bibliohashoka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

How To Generate PDF Invoice Using Java - CodeSpeedy

Uploaded by

bibliohashoka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

(https://www.codespeedy.com/)

Home Online Python Compiler Online Swift Compiler Contact

Search
How to generate PDF invoice using
Java Latest Articles

By Tishya Thakkar (https://www.codespeedy.com/author/tishya_thakkar/) How to create charts in


SwiftUI
In this tutorial, we will look at how to generate an invoice
Create a dropdown menu
using Java.
in SwiftUI

What is an invoice? An invoice is a document that contains Delete a Button after it is


information regarding a sale transaction like products, pressed in SwiftUI
quantities, prices, etc. which is issued by the seller to the Gradient Border in SwiftUI
buyer.
How to remove blank
lines from a .txt file in
Since we are creating the invoice as a PDF we will use an
Node.js
open-source Java library know as PDFBox.

Steps to get started with PDFBox:

1. Download the latest PDFBox JAR from


https://pdfbox.apache.org/download.html.
2. Open eclipse and create a new Java project.
https://www.codespeedy.com/generate-pdf-invoice-using-java/ 1/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

3. Right-click on the project then go to Build path and then click


on Configure build path.
4. Go to libraries and then click on Add External JARs and
select the downloaded PDFBox JAR file.
5. Click on apply and close.

Creating the PDF


To create a PDF we use the PDDocument class from
PDFBox.

1.
2.
//Creating the PDF
PDDocument MyPDF = new PDDocument(); C# PDF
Library
Adding a blank page .NET applications edit, convert, render, print
PDF documents.
To add a blank page to our invoice PDF we will first use the
class PDPage to create a blank page and then use the E-ICEBLUE
addPage() method to add the blank page to our invoice PDF.

1. //Creating a Blank Page


2. PDPage newpage = new PDPage();
3. //Adding the blank page to our PDF
4. MyPDF.addPage(newpage);

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 2/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

Adding text to the PDF


There are two ways of adding text to our invoice PDF. One is
by adding a single line and the other is by adding multiple
lines.

To add a single line of text we use the following steps:

1. Get the required page from the PDF using the


getPage() method. This method takes the index of the
page as the parameter.
1. //Getting the required Page
2. //0 index for the first page
3. PDPage mypage = MyPDF.getPage(0);

2. Initialize the content stream using the


PDPageContentStream class as shown.
1. //Initializing the content stream
2. PDPageContentStream cs = new PDPageContentS

3. Before we can write the text we have to use the


beginText() method and then set the position of the text
and its font using the methods newLineAtOffset() and
setFont() respectively.
1. cs.beginText();
https://www.codespeedy.com/generate-pdf-invoice-using-java/ 3/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

2. cs.setFont(PDType1Font.TIMES_ROMAN, 18);
3. cs.newLineAtOffset(150, 750);

4. To write the text into the PDF we use the showText()


method.
1. //String variable to store the text
2. String InvoiceTitle = new String("CodeSpeed
3. //Writing the text to the PDF Fie
4. cs.showText(InvoiceTitle);

5. Lastly, we have to end the text and close the content


stream using the methods endText() and close()
respectively.
1. //Ending the text
2. cs.endText();
3. //Closing the content stream
4. cs.close();

To add multiple lines of text we use the following steps:

1. Get the required page from the PDF using the


getPage() method. This method takes the index of the
page as the parameter.
1. //Getting the required Page
2. //0 index for the first page

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 4/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

3. PDPage mypage = MyPDF.getPage(0);

2. Initialize the content stream using the


PDPageContentStream class as shown.
1. //Initializing the content stream
2. PDPageContentStream cs = new PDPageContentS

3. Before we can write the text we have to use the


beginText() method and then set the position of the text
and its font using the methods newLineAtOffset() and
setFont() respectively.
1. cs.beginText();
2. cs.setFont(PDType1Font.TIMES_ROMAN, 18);
3. cs.newLineAtOffset(150, 750);

4. Next, we have to set the leading text. we the method


setLeading() to specify how much to move down to get
the next line.
1. //Setting the leading line
2. cs.setLeading(15f);

5. Now we can start inserting multiple lines of text using


the methods showText() and newline().
1. cs.showText("Text Line 1");
2. cs.newLine();
3. cs.showText("Text Line 2");

6. Lastly, we have to end the text and close the content


stream using the methods endText() and close()
https://www.codespeedy.com/generate-pdf-invoice-using-java/ 5/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

respectively.
1. //Ending the text
2. cs.endText();
3. //Closing the content stream
4. cs.close();

Saving the PDF


To save the PDF we use the save() method and pass the
path (along with the PDF file name) to save the document as
a string value in the method parameter.

1. //Saving the PDF


2. MyPDF.save(String FilePath);

Invoice PDF generator


Now, let us look at an example code to generate a PDF
invoice using the above-mentioned classes and methods. In
this code, we are using a constructor to create a pdf with a
blank page. Next using the getdata() method we collect the
necessary information required like the customer details and

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 6/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

the product details. To write the invoice we are using the


method WriteInvoice(). This method uses both single line and
multiple lines methods to write the text to the PDF invoice.

1. import java.io.IOException;
2. import java.util.ArrayList;
3. import java.util.List;
4. import java.util.Scanner;
5.
6. import org.apache.pdfbox.pdmodel.PDDocument;
7. import org.apache.pdfbox.pdmodel.PDPage;
8. import org.apache.pdfbox.pdmodel.PDPageContentStream
9. import org.apache.pdfbox.pdmodel.font.PDType1Font;
10.
11. public class invoice {
12. PDDocument invc;
13. int n;
14. Integer total = 0;
15. Integer price;
16. String CustName;
17. String CustPh;
18. List<String> ProductName = new ArrayList<String>(
19. List<Integer> ProductPrice = new ArrayList<Intege
20. List<Integer> ProductQty = new ArrayList<Integer>
21. String InvoiceTitle = new String("CodeSpeedy Tech
22. String SubTitle = new String("Invoice");
23.
24. //Using the constructor to create a PDF with a bla
25. invoice() {
26. //Create Document
27. invc = new PDDocument();
28. //Create Blank Page

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 7/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

29. PDPage newpage = new PDPage();


30. //Add the blank page
31. invc.addPage(newpage);
32. }
33.
34. //getdata() method is used to get the customer inf
35. void getdata() {
36. Scanner sc = new Scanner(System.in);
37. System.out.println("Enter the Customer Name: ")
38. CustName = sc.nextLine();
39. System.out.println("Enter the Customer Phone Num
40. CustPh = sc.next();
41. System.out.println("Enter the Number of Product
42. n = sc.nextInt();
43. System.out.println();
44. for(int i=0; i<n; i++) {
45. System.out.println("Enter the Product Name: "
46. ProductName.add(sc.next());
47. System.out.println("Enter the Price of the Pro
48. ProductPrice.add(sc.nextInt());
49. System.out.println("Enter the Quantity of the
50. ProductQty.add(sc.nextInt());
51. System.out.println();
52. //Calculating the total amount
53. total = total + (ProductPrice.get(i)*ProductQt
54. }
55. }
56.
57. void WriteInvoice() {
58. //get the page
59. PDPage mypage = invc.getPage(0);
60. try {
61. //Prepare Content Stream

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 8/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

62. PDPageContentStream cs = new PDPageContentStre


63.
64. //Writing Single Line text
65. //Writing the Invoice title
66. cs.beginText();
67. cs.setFont(PDType1Font.TIMES_ROMAN, 20);
68. cs.newLineAtOffset(140, 750);
69. cs.showText(InvoiceTitle);
70. cs.endText();
71.
72. cs.beginText();
73. cs.setFont(PDType1Font.TIMES_ROMAN, 18);
74. cs.newLineAtOffset(270, 690);
75. cs.showText(SubTitle);
76. cs.endText();
77.
78. //Writing Multiple Lines
79. //writing the customer details
80. cs.beginText();
81. cs.setFont(PDType1Font.TIMES_ROMAN, 14);
82. cs.setLeading(20f);
83. cs.newLineAtOffset(60, 610);
84. cs.showText("Customer Name: ");
85. cs.newLine();
86. cs.showText("Phone Number: ");
87. cs.endText();
88.
89. cs.beginText();
90. cs.setFont(PDType1Font.TIMES_ROMAN, 14);
91. cs.setLeading(20f);
92. cs.newLineAtOffset(170, 610);
93. cs.showText(CustName);
94. cs.newLine();

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 9/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

95. cs.showText(CustPh);
96. cs.endText();
97.
98. cs.beginText();
99. cs.setFont(PDType1Font.TIMES_ROMAN, 14);
100. cs.newLineAtOffset(80, 540);
101. cs.showText("Product Name");
102. cs.endText();
103.
104. cs.beginText();
105. cs.setFont(PDType1Font.TIMES_ROMAN, 14);
106. cs.newLineAtOffset(200, 540);
107. cs.showText("Unit Price");
108. cs.endText();
109.
110. cs.beginText();
111. cs.setFont(PDType1Font.TIMES_ROMAN, 14);
112. cs.newLineAtOffset(310, 540);
113. cs.showText("Quantity");
114. cs.endText();
115.
116. cs.beginText();
117. cs.setFont(PDType1Font.TIMES_ROMAN, 14);
118. cs.newLineAtOffset(410, 540);
119. cs.showText("Price");
120. cs.endText();
121.
122. cs.beginText();
123. cs.setFont(PDType1Font.TIMES_ROMAN, 12);
124. cs.setLeading(20f);
125. cs.newLineAtOffset(80, 520);
126. for(int i =0; i<n; i++) {
127. cs.showText(ProductName.get(i));

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 10/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

128. cs.newLine();
129. }
130. cs.endText();
131.
132. cs.beginText();
133. cs.setFont(PDType1Font.TIMES_ROMAN, 12);
134. cs.setLeading(20f);
135. cs.newLineAtOffset(200, 520);
136. for(int i =0; i<n; i++) {
137. cs.showText(ProductPrice.get(i).toString())
138. cs.newLine();
139. }
140. cs.endText();
141.
142. cs.beginText();
143. cs.setFont(PDType1Font.TIMES_ROMAN, 12);
144. cs.setLeading(20f);
145. cs.newLineAtOffset(310, 520);
146. for(int i =0; i<n; i++) {
147. cs.showText(ProductQty.get(i).toString());
148. cs.newLine();
149. }
150. cs.endText();
151.
152. cs.beginText();
153. cs.setFont(PDType1Font.TIMES_ROMAN, 12);
154. cs.setLeading(20f);
155. cs.newLineAtOffset(410, 520);
156. for(int i =0; i<n; i++) {
157. price = ProductPrice.get(i)*ProductQty.get(
158. cs.showText(price.toString());
159. cs.newLine();
160. }

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 11/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

161. cs.endText();
162.
163. cs.beginText();
164. cs.setFont(PDType1Font.TIMES_ROMAN, 14);
165. cs.newLineAtOffset(310, (500-(20*n)));
166. cs.showText("Total: ");
167. cs.endText();
168.
169. cs.beginText();
170. cs.setFont(PDType1Font.TIMES_ROMAN, 14);
171. //Calculating where total is to be written us
172. cs.newLineAtOffset(410, (500-(20*n)));
173. cs.showText(total.toString());
174. cs.endText();
175.
176. //Close the content stream
177. cs.close();
178. //Save the PDF
179. invc.save(String FilePath);
180.
181. } catch (IOException e) {
182. e.printStackTrace();
183. }
184. }
185.
186.
187. public static void main(String args[]) {
188. invoice i = new invoice();
189. i.getdata();
190. i.WriteInvoice();
191. System.out.println("Invoice Generated!");
192. }
193. }

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 12/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

Output

Enter the Customer Name:


James
Enter the Customer Phone Number:
9849576899
Enter the Number of Products:
3

Enter the Product Name:


Product1
Enter the Price of the Product:
150
Enter the Quantity of the Product:
3

Enter the Product Name:


Product2
Enter the Price of the Product:
130
Enter the Quantity of the Product:
1

Enter the Product Name:


Product3
Enter the Price of the Product:
150
Enter the Quantity of the Product:
1

The invoice will look like this:

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 13/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 14/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

One response to “How to generate PDF invoice using


Java”
1. Vineta says:
February 7, 2022 at 4:12 pm
(https://www.codespeedy.com/generate-pdf-invoice-using-
java/#comment-6838)
Hi! Thanks! It works and I will try to modify it for my needs. But the scanner
is never closed. Where should the sc.close() go?

Best regards,
Vineta

Reply

Leave a Reply
Your email address will not be published. Required fields are
marked *

Comment *

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 15/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

Name *

Email *

Je ne suis pas un robot


reCAPTCHA
Confidentialité - Conditions

Post Comment

« How to find the largest file in a folder in How to calculate velocity using Java
C++ (https://www.codespeedy.com/find- (https://www.codespeedy.com/calculate-
the-largest-file-in-a-folder-in-c/) velocity-using-java/) »

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 16/17
13/08/2023 19:44 How to generate PDF invoice using Java - CodeSpeedy

Python By continuing to visit our


website, you agree to
(https://www.codespeedy.com/category/python/)
| Java the use of cookies as
(https://www.codespeedy.com/category/java/)
described in our Cookie
| C++
Policy
(https://www.codespeedy.com/category/cpp-
programming/) |Machine
(https://www.codespeedy.com/cookie-
Learning policy/)
(https://www.codespeedy.com/category/machine-
learning/) | JavaScript About Us
(https://www.codespeedy.com/about-
(https://www.codespeedy.com/category/javascript/)
us/) | Contact
Privacy policy (https://www.codespeedy.com/contact-
us/)
(https://www.codespeedy.com/privacy-
policy/) | Contact Buyer Terms and Conditions &
Privacy Policy
(https://www.codespeedy.com/contact-
(https://www.codespeedy.com/buyer-
us/) | About
terms-and-conditions-and-
(https://www.codespeedy.com/about-
privacy-policy/)
us/)
Refund Policy
(https://www.codespeedy.com/buyer-
terms-and-conditions-and-
privacy-policy/#refund-policy)

https://www.codespeedy.com/generate-pdf-invoice-using-java/ 17/17

You might also like