6

I'm working on small project in java, there I want to fetch the contents from a database and write them into a PDF file.

I tried to googling and came up with iText Library.

Can anyone guide to create a PDF that looks like the enclosed image computer generated invoice

PS: I'm pretty new to JAVA.and it's my first java project.

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
Ashu_FalcoN
  • 916
  • 2
  • 10
  • 21
  • 2
    This question is too broad for Stack Overflow. Start by reading [the documentation](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-4-creating-reports-using-pdfhtml) (if you scroll down, you'll see an invoice example). Start coding, and return to Stack Overflow when you have a *specific technical problem*. Stack Overflow isn't a "do my work for me" platform, nor a learning platform. – Bruno Lowagie Sep 17 '17 at 12:44
  • 1
    And of course, there's a complete book dedicated to producing invoices with iText: https://developers.itextpdf.com/content/zugferd-future-invoicing – Bruno Lowagie Sep 17 '17 at 12:53
  • @BrunoLowagie thanks for the feedback :) i'm just looking for starter reference.. – Ashu_FalcoN Sep 17 '17 at 14:00
  • Plenty of reference material here: https://developers.itextpdf.com/books – Bruno Lowagie Sep 17 '17 at 15:35
  • have you got any solution for this?? – Rajnish Sharma Oct 27 '20 at 13:57

1 Answers1

12

I've done a quick implementation of most of your use-case.

Here's the code:
First we define a small class that acts as a single record in the invoice.

static class Article{
    int SNO;
    String description;
    int quantity;
    double unitPrice;
    public Article(int SNO, String description, int quantity, double unitPrice)
    {
        this.SNO = SNO;
        this.description = description;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
    }
}

Then I've created a method for each of the big blocks in the invoice.
Starting with the title:

public static void addTitle(Document layoutDocument)
{
    layoutDocument.add(new Paragraph("RETAIL INVOICE").setBold().setUnderline().setTextAlignment(TextAlignment.CENTER));
}

Then adding the little paragraph of text that's underneath the title:

public static void addCustomerReference(Document layoutDocument)
{
    layoutDocument.add(new Paragraph("M/s Indian Convent School").setTextAlignment(TextAlignment.LEFT).setMultipliedLeading(0.2f));
    layoutDocument.add(new Paragraph("y Pocket-3, Sector-24, Rohini Delhi-110085").setMultipliedLeading(.2f));
    layoutDocument.add(new Paragraph("b 011-64660271").setMultipliedLeading(.2f));
}

And then adding a table:

public void addTable(Document layoutDocument, List<Article> articleList)
{
    Table table = new Table(UnitValue.createPointArray(new float[]{60f, 180f, 50f, 80f, 110f}));

    // headers
    table.addCell(new Paragraph("S.N.O.").setBold());
    table.addCell(new Paragraph("PARTICULARS").setBold());
    table.addCell(new Paragraph("QTY").setBold());
    table.addCell(new Paragraph("RATE").setBold());
    table.addCell(new Paragraph("AMOUNT IN RS.").setBold());

    // items
    for(Article a : articleList)
    {
        table.addCell(new Paragraph(a.SNO+""));
        table.addCell(new Paragraph(a.description));
        table.addCell(new Paragraph(a.quantity+""));
        table.addCell(new Paragraph(a.unitPrice+""));
        table.addCell(new Paragraph((a.quantity * a.unitPrice)+""));
    }

    layoutDocument.add(table);
}

The main method then looks like this:

public static void main(String[] args) throws FileNotFoundException {

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter("MyFirstInvoice.pdf"));
    Document layoutDocument = new Document(pdfDocument);

    // title
    addTitle(layoutDocument);

    // customer reference information
    addCustomerReference(layoutDocument);
    addTable(layoutDocument, Arrays.asList(
            new Article(1, "Envelopes",2000, 1.70),
            new Article(2, "Voucher Book", 50, 41)));

    // articles
    layoutDocument.close();
}
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
  • HI can you provide an example I can not implement this – Ilia Tapia Nov 18 '19 at 11:16
  • 1
    There is a complete example in my post – Joris Schellekens Nov 18 '19 at 20:42
  • Hi there, can you provide more, I tried your example but I couldn't make it work. I could not find libraries and much other error, and if you can provide me an example after I generate pdf to send this pdf via email, I would really appreciate that.. as i am stuck i am using spring and java 8 @Joris – Ilia Tapia Nov 19 '19 at 09:58
  • What errors are you getting? The example should work as is. – Joris Schellekens Nov 19 '19 at 14:40
  • Yeah but most of them are not know PdfDocument, for example, the table also I have tried to import every possible opportunity but none of them support addCell() – Ilia Tapia Nov 20 '19 at 08:44
  • Then your imports are wrong. It sounds like you're including the old version of iText (5 or earlier, rather than 7) – Joris Schellekens Nov 20 '19 at 09:42
  • I have already imported the newest one but still, don't work and also I have imported and check the valid opportunities and still the same. Maybe is the way I have done it , but I don't think so as I am not using your main void method but I am do it from service, as i also need to send it via email – Ilia Tapia Nov 20 '19 at 10:26
  • PdfDocument is clearly a class in the iText API. You can check their documentation to verify that. So, as I said, your imports are wrong. https://api.itextpdf.com/iText7/java/7.1.8/com/itextpdf/kernel/pdf/PdfDocument.html – Joris Schellekens Nov 20 '19 at 10:42
  • do you have a link to the post? – chia yongkang Nov 30 '19 at 02:09
  • @IliaTapia you might be using itextg5, add this dependency instead----> implementation 'com.itextpdf:itext7-core:7.1.12' – Raj Sharma Sep 10 '20 at 04:32
  • https://stackoverflow.com/questions/43607530/import-itext-7-in-android-gradle – Raj Sharma Sep 10 '20 at 04:32