Subversion Repositories SmartDukaan

Rev

Rev 37062 | 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;
35024 amit 10
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
37066 amit 11
import com.spice.profitmandi.common.model.CreditNotePdfModel;
12
import com.spice.profitmandi.common.model.DebitNotePdfModel;
13
import com.spice.profitmandi.common.model.InvoicePdfModel;
29930 amit.gupta 14
 
24506 amit.gupta 15
import java.io.ByteArrayInputStream;
16
import java.io.ByteArrayOutputStream;
17
import java.io.IOException;
21686 ashik.ali 18
import java.io.OutputStream;
24854 amit.gupta 19
import java.util.ArrayList;
23001 amit.gupta 20
import java.util.List;
21686 ashik.ali 21
 
37066 amit 22
/**
23
 * Entry points for invoice/note PDF generation. All A4 document rendering is delegated to
24
 * {@link DocumentRenderer} (package {@code com.spice.profitmandi.common.document}); these methods
25
 * are thin adapters kept for their existing callers. Thermal (58/80mm) printing still goes through
26
 * {@link InvoiceFormatter}.
27
 */
21686 ashik.ali 28
public class PdfUtils {
23509 amit.gupta 29
 
32275 tejbeer 30
    public static final String INVOICE_TITLE = "TAX INVOICE";
31
    public static final String DEBIT_NOTE_TITLE = "DEBIT NOTE";
32
    public static final String SECURITY_DEPOSIT = "SECURITY DEPOSIT RECEIPT";
23509 amit.gupta 33
 
37066 amit 34
    /** A4 → full document renderer; 58/80mm → thermal formatter. */
35035 amit 35
    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, PrinterType printerType, ByteArrayOutputStream outputStream) throws ProfitMandiBusinessException {
35024 amit 36
        if (PrinterType.A4.equals(printerType)) {
37
            generateAndWrite(pdfModels, outputStream);
38
        } else {
39
            if (PrinterType.W80.equals(printerType)) {
40
                InvoiceFormatter.getInvoice(pdfModels.get(0), outputStream, InvoiceFormatter.WIDTH_80MM);
41
            } else if (PrinterType.W58.equals(printerType)) {
42
                InvoiceFormatter.getInvoice(pdfModels.get(0), outputStream, InvoiceFormatter.WIDTH_58MM);
43
            }
44
        }
35035 amit 45
    }
35024 amit 46
 
32275 tejbeer 47
    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
37066 amit 48
        List<DocumentData> docs = new ArrayList<>();
49
        for (InvoicePdfModel m : pdfModels) docs.add(DocumentDataAdapter.from(m));
50
        new DocumentRenderer().render(docs, outputStream);
32275 tejbeer 51
    }
23509 amit.gupta 52
 
36109 amit 53
    public static void generateMarginSchemeInvoice(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
36116 amit 54
        generateInvoiceV2(pdfModels, outputStream);
55
    }
56
 
57
    public static void generateInvoiceV2(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
37066 amit 58
        List<DocumentData> docs = new ArrayList<>();
59
        for (InvoicePdfModel m : pdfModels) docs.add(DocumentDataAdapter.from(m));
60
        new DocumentRenderer().render(docs, outputStream);
36109 amit 61
    }
62
 
32275 tejbeer 63
    public static void generateAndWriteDebitNote(List<DebitNotePdfModel> debitNotePdfModels, OutputStream outputStream) {
37066 amit 64
        List<DocumentData> docs = new ArrayList<>();
65
        for (DebitNotePdfModel m : debitNotePdfModels) docs.add(DocumentDataAdapter.from(m));
66
        new DocumentRenderer().render(docs, outputStream);
32275 tejbeer 67
    }
23509 amit.gupta 68
 
32275 tejbeer 69
    public static void generateAndWriteCustomerCreditNotes(List<CreditNotePdfModel> creditNotes, OutputStream outputStream) {
37066 amit 70
        List<DocumentData> docs = new ArrayList<>();
71
        for (CreditNotePdfModel m : creditNotes) docs.add(DocumentDataAdapter.from(m));
72
        new DocumentRenderer().render(docs, outputStream);
32275 tejbeer 73
    }
34805 ranu 74
 
75
    public static byte[] mergePdfFiles(List<byte[]> pdfFiles) throws IOException, DocumentException {
76
        ByteArrayOutputStream mergedOutputStream = new ByteArrayOutputStream();
77
        Document document = new Document();
78
        PdfCopy copy = new PdfCopy(document, mergedOutputStream);
79
        document.open();
80
 
81
        for (byte[] pdf : pdfFiles) {
82
            PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
83
            int n = reader.getNumberOfPages();
84
            for (int i = 1; i <= n; i++) {
85
                copy.addPage(copy.getImportedPage(reader, i));
86
            }
87
            copy.freeReader(reader);
88
            reader.close();
89
        }
90
 
91
        document.close();
92
        return mergedOutputStream.toByteArray();
93
    }
21686 ashik.ali 94
}