Subversion Repositories SmartDukaan

Rev

Rev 36674 | Rev 37062 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.spice.profitmandi.common.document.DocumentData;
import com.spice.profitmandi.common.document.DocumentDataAdapter;
import com.spice.profitmandi.common.document.DocumentRenderer;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CreditNotePdfModel;
import com.spice.profitmandi.common.model.DebitNotePdfModel;
import com.spice.profitmandi.common.model.InvoicePdfModel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Entry points for invoice/note PDF generation. All A4 document rendering is delegated to
 * {@link DocumentRenderer} (package {@code com.spice.profitmandi.common.document}); these methods
 * are thin adapters kept for their existing callers. Thermal (58/80mm) printing still goes through
 * {@link InvoiceFormatter}.
 */
public class PdfUtils {

    public static final String INVOICE_TITLE = "TAX INVOICE";
    public static final String DEBIT_NOTE_TITLE = "DEBIT NOTE";
    public static final String SECURITY_DEPOSIT = "SECURITY DEPOSIT RECEIPT";

    /** A4 → full document renderer; 58/80mm → thermal formatter. */
    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, PrinterType printerType, ByteArrayOutputStream outputStream) throws ProfitMandiBusinessException {
        if (PrinterType.A4.equals(printerType)) {
            generateAndWrite(pdfModels, outputStream);
        } else {
            if (PrinterType.W80.equals(printerType)) {
                InvoiceFormatter.getInvoice(pdfModels.get(0), outputStream, InvoiceFormatter.WIDTH_80MM);
            } else if (PrinterType.W58.equals(printerType)) {
                InvoiceFormatter.getInvoice(pdfModels.get(0), outputStream, InvoiceFormatter.WIDTH_58MM);
            }
        }
    }

    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
        List<DocumentData> docs = new ArrayList<>();
        for (InvoicePdfModel m : pdfModels) docs.add(DocumentDataAdapter.from(m));
        new DocumentRenderer().render(docs, outputStream);
    }

    public static void generateMarginSchemeInvoice(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
        generateInvoiceV2(pdfModels, outputStream);
    }

    public static void generateInvoiceV2(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
        List<DocumentData> docs = new ArrayList<>();
        for (InvoicePdfModel m : pdfModels) docs.add(DocumentDataAdapter.from(m));
        new DocumentRenderer().render(docs, outputStream);
    }

    public static void generateAndWriteDebitNote(List<DebitNotePdfModel> debitNotePdfModels, OutputStream outputStream) {
        List<DocumentData> docs = new ArrayList<>();
        for (DebitNotePdfModel m : debitNotePdfModels) docs.add(DocumentDataAdapter.from(m));
        new DocumentRenderer().render(docs, outputStream);
    }

    public static void generateAndWriteCustomerCreditNotes(List<CreditNotePdfModel> creditNotes, OutputStream outputStream) {
        List<DocumentData> docs = new ArrayList<>();
        for (CreditNotePdfModel m : creditNotes) docs.add(DocumentDataAdapter.from(m));
        new DocumentRenderer().render(docs, outputStream);
    }

    public static byte[] mergePdfFiles(List<byte[]> pdfFiles) throws IOException, DocumentException {
        ByteArrayOutputStream mergedOutputStream = new ByteArrayOutputStream();
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, mergedOutputStream);
        document.open();

        for (byte[] pdf : pdfFiles) {
            PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
            int n = reader.getNumberOfPages();
            for (int i = 1; i <= n; i++) {
                copy.addPage(copy.getImportedPage(reader, i));
            }
            copy.freeReader(reader);
            reader.close();
        }

        document.close();
        return mergedOutputStream.toByteArray();
    }
}