Subversion Repositories SmartDukaan

Rev

Rev 37066 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21686 ashik.ali 1
package com.spice.profitmandi.common.util;
2
 
37066 amit 3
import com.itextpdf.text.Document;
4
import com.itextpdf.text.DocumentException;
5
import com.itextpdf.text.pdf.PdfCopy;
6
import com.itextpdf.text.pdf.PdfReader;
7
import com.spice.profitmandi.common.document.DocumentData;
8
import com.spice.profitmandi.common.document.DocumentDataAdapter;
9
import com.spice.profitmandi.common.document.DocumentRenderer;
10
import com.spice.profitmandi.common.model.CreditNotePdfModel;
11
import com.spice.profitmandi.common.model.DebitNotePdfModel;
12
import com.spice.profitmandi.common.model.InvoicePdfModel;
29930 amit.gupta 13
 
24506 amit.gupta 14
import java.io.ByteArrayInputStream;
15
import java.io.ByteArrayOutputStream;
16
import java.io.IOException;
21686 ashik.ali 17
import java.io.OutputStream;
24854 amit.gupta 18
import java.util.ArrayList;
23001 amit.gupta 19
import java.util.List;
21686 ashik.ali 20
 
37066 amit 21
/**
37397 amit 22
 * Entry points for invoice/note PDF generation. All document rendering is delegated to
37066 amit 23
 * {@link DocumentRenderer} (package {@code com.spice.profitmandi.common.document}); these methods
37397 amit 24
 * are thin adapters kept for their existing callers.
37066 amit 25
 */
21686 ashik.ali 26
public class PdfUtils {
23509 amit.gupta 27
 
32275 tejbeer 28
    public static final String INVOICE_TITLE = "TAX INVOICE";
29
    public static final String DEBIT_NOTE_TITLE = "DEBIT NOTE";
30
    public static final String SECURITY_DEPOSIT = "SECURITY DEPOSIT RECEIPT";
23509 amit.gupta 31
 
32275 tejbeer 32
    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
37066 amit 33
        List<DocumentData> docs = new ArrayList<>();
34
        for (InvoicePdfModel m : pdfModels) docs.add(DocumentDataAdapter.from(m));
35
        new DocumentRenderer().render(docs, outputStream);
32275 tejbeer 36
    }
23509 amit.gupta 37
 
36116 amit 38
    public static void generateInvoiceV2(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
37066 amit 39
        List<DocumentData> docs = new ArrayList<>();
40
        for (InvoicePdfModel m : pdfModels) docs.add(DocumentDataAdapter.from(m));
41
        new DocumentRenderer().render(docs, outputStream);
36109 amit 42
    }
43
 
32275 tejbeer 44
    public static void generateAndWriteDebitNote(List<DebitNotePdfModel> debitNotePdfModels, OutputStream outputStream) {
37066 amit 45
        List<DocumentData> docs = new ArrayList<>();
46
        for (DebitNotePdfModel m : debitNotePdfModels) docs.add(DocumentDataAdapter.from(m));
47
        new DocumentRenderer().render(docs, outputStream);
32275 tejbeer 48
    }
23509 amit.gupta 49
 
32275 tejbeer 50
    public static void generateAndWriteCustomerCreditNotes(List<CreditNotePdfModel> creditNotes, OutputStream outputStream) {
37066 amit 51
        List<DocumentData> docs = new ArrayList<>();
52
        for (CreditNotePdfModel m : creditNotes) docs.add(DocumentDataAdapter.from(m));
53
        new DocumentRenderer().render(docs, outputStream);
32275 tejbeer 54
    }
34805 ranu 55
 
56
    public static byte[] mergePdfFiles(List<byte[]> pdfFiles) throws IOException, DocumentException {
57
        ByteArrayOutputStream mergedOutputStream = new ByteArrayOutputStream();
58
        Document document = new Document();
59
        PdfCopy copy = new PdfCopy(document, mergedOutputStream);
60
        document.open();
61
 
62
        for (byte[] pdf : pdfFiles) {
63
            PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
64
            int n = reader.getNumberOfPages();
65
            for (int i = 1; i <= n; i++) {
66
                copy.addPage(copy.getImportedPage(reader, i));
67
            }
68
            copy.freeReader(reader);
69
            reader.close();
70
        }
71
 
72
        document.close();
73
        return mergedOutputStream.toByteArray();
74
    }
21686 ashik.ali 75
}