Subversion Repositories SmartDukaan

Rev

Rev 37066 | 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.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 document rendering is delegated to
 * {@link DocumentRenderer} (package {@code com.spice.profitmandi.common.document}); these methods
 * are thin adapters kept for their existing callers.
 */
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";

    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 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();
    }
}