How to generate PDF invoice using Java

In this tutorial, we will look at how to generate an invoice using Java.

What is an invoice? An invoice is a document that contains information regarding a sale transaction like products, quantities, prices, etc. which is issued by the seller to the buyer.

Since we are creating the invoice as a PDF we will use an 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.
  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.

//Creating the PDF PDDocument MyPDF = new PDDocument();

Adding a blank page

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 addPage() method to add the blank page to our invoice PDF.

//Creating a Blank Page PDPage newpage = new PDPage(); //Adding the blank page to our PDF MyPDF.addPage(newpage);

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:

      Get the required page from the PDF using the getPage() method. This method takes the index of the page as the parameter.

    //Getting the required Page //0 index for the first page PDPage mypage = MyPDF.getPage(0);
    //Initializing the content stream PDPageContentStream cs = new PDPageContentStream(MyPDF, MyPage);
    cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 18); cs.newLineAtOffset(150, 750);
    //String variable to store the text String InvoiceTitle = new String("CodeSpeedy Technology Private Limited"); //Writing the text to the PDF Fie cs.showText(InvoiceTitle);
    //Ending the text cs.endText(); //Closing the content stream cs.close();

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

        Get the required page from the PDF using the getPage() method. This method takes the index of the page as the parameter.

      //Getting the required Page //0 index for the first page PDPage mypage = MyPDF.getPage(0);
      //Initializing the content stream PDPageContentStream cs = new PDPageContentStream(MyPDF, MyPage);
      cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 18); cs.newLineAtOffset(150, 750);
      //Setting the leading line cs.setLeading(15f);
      cs.showText("Text Line 1"); cs.newLine(); cs.showText("Text Line 2");
      //Ending the text cs.endText(); //Closing the content stream 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.

      //Saving the PDF 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 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.

      import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class invoice < PDDocument invc; int n; Integer total = 0; Integer price; String CustName; String CustPh; ListProductName = new ArrayList(); List ProductPrice = new ArrayList(); List ProductQty = new ArrayList(); String InvoiceTitle = new String("CodeSpeedy Technology Private Limited"); String SubTitle = new String("Invoice"); //Using the constructor to create a PDF with a blank page invoice() < //Create Document invc = new PDDocument(); //Create Blank Page PDPage newpage = new PDPage(); //Add the blank page invc.addPage(newpage); >//getdata() method is used to get the customer information and the product details from the input stream void getdata() < Scanner sc = new Scanner(System.in); System.out.println("Enter the Customer Name: "); CustName = sc.nextLine(); System.out.println("Enter the Customer Phone Number: "); CustPh = sc.next(); System.out.println("Enter the Number of Products: "); n = sc.nextInt(); System.out.println(); for(int i=0; i> void WriteInvoice() < //get the page PDPage mypage = invc.getPage(0); try < //Prepare Content Stream PDPageContentStream cs = new PDPageContentStream(invc, mypage); //Writing Single Line text //Writing the Invoice title cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 20); cs.newLineAtOffset(140, 750); cs.showText(InvoiceTitle); cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 18); cs.newLineAtOffset(270, 690); cs.showText(SubTitle); cs.endText(); //Writing Multiple Lines //writing the customer details cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 14); cs.setLeading(20f); cs.newLineAtOffset(60, 610); cs.showText("Customer Name: "); cs.newLine(); cs.showText("Phone Number: "); cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 14); cs.setLeading(20f); cs.newLineAtOffset(170, 610); cs.showText(CustName); cs.newLine(); cs.showText(CustPh); cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 14); cs.newLineAtOffset(80, 540); cs.showText("Product Name"); cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 14); cs.newLineAtOffset(200, 540); cs.showText("Unit Price"); cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 14); cs.newLineAtOffset(310, 540); cs.showText("Quantity"); cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 14); cs.newLineAtOffset(410, 540); cs.showText("Price"); cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 12); cs.setLeading(20f); cs.newLineAtOffset(80, 520); for(int i =0; ics.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 12); cs.setLeading(20f); cs.newLineAtOffset(200, 520); for(int i =0; i cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 12); cs.setLeading(20f); cs.newLineAtOffset(310, 520); for(int i =0; i cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 12); cs.setLeading(20f); cs.newLineAtOffset(410, 520); for(int i =0; i cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 14); cs.newLineAtOffset(310, (500-(20*n))); cs.showText("Total: "); cs.endText(); cs.beginText(); cs.setFont(PDType1Font.TIMES_ROMAN, 14); //Calculating where total is to be written using number of products cs.newLineAtOffset(410, (500-(20*n))); cs.showText(total.toString()); cs.endText(); //Close the content stream cs.close(); //Save the PDF invc.save(String FilePath); > catch (IOException e) < e.printStackTrace(); >> public static void main(String args[]) < invoice i = new invoice(); i.getdata(); i.WriteInvoice(); System.out.println("Invoice Generated!"); >>

      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: