| 37066 |
amit |
1 |
package com.spice.profitmandi.common.document;
|
|
|
2 |
|
|
|
3 |
import com.itextpdf.text.Document;
|
|
|
4 |
import com.itextpdf.text.DocumentException;
|
|
|
5 |
import com.itextpdf.text.PageSize;
|
|
|
6 |
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
7 |
|
|
|
8 |
import com.spice.profitmandi.common.document.section.FooterSection;
|
|
|
9 |
import com.spice.profitmandi.common.document.section.HeaderSection;
|
|
|
10 |
import com.spice.profitmandi.common.document.section.ItemsTableSection;
|
|
|
11 |
import com.spice.profitmandi.common.document.section.PartiesSection;
|
|
|
12 |
import com.spice.profitmandi.common.document.section.Section;
|
|
|
13 |
import com.spice.profitmandi.common.document.section.SummarySection;
|
|
|
14 |
import com.spice.profitmandi.common.document.support.CancelledStamp;
|
|
|
15 |
import com.spice.profitmandi.common.util.EWayBillPDF;
|
|
|
16 |
import org.apache.logging.log4j.LogManager;
|
|
|
17 |
import org.apache.logging.log4j.Logger;
|
|
|
18 |
|
|
|
19 |
import java.io.ByteArrayOutputStream;
|
|
|
20 |
import java.io.OutputStream;
|
|
|
21 |
import java.util.ArrayList;
|
|
|
22 |
import java.util.Arrays;
|
|
|
23 |
import java.util.List;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Renders a list of {@link DocumentData} into a single PDF by driving the canonical section
|
|
|
27 |
* pipeline (V2's look) for each document. Replaces the duplicated bodies of the legacy
|
|
|
28 |
* {@code generateInvoiceV2 / generateAndWrite / generateAndWriteDebitNote /
|
|
|
29 |
* generateAndWriteCustomerCreditNotes} methods.
|
|
|
30 |
*/
|
|
|
31 |
public class DocumentRenderer {
|
|
|
32 |
|
|
|
33 |
private static final Logger LOGGER = LogManager.getLogger(DocumentRenderer.class);
|
|
|
34 |
|
|
|
35 |
// Stateless, reusable. Order defines the page layout.
|
|
|
36 |
private static final List<Section> SECTIONS = Arrays.asList(
|
|
|
37 |
new HeaderSection(),
|
|
|
38 |
new PartiesSection(),
|
|
|
39 |
new ItemsTableSection(),
|
|
|
40 |
new SummarySection(),
|
|
|
41 |
new FooterSection());
|
|
|
42 |
|
|
|
43 |
public void render(List<DocumentData> docs, OutputStream out) {
|
|
|
44 |
try {
|
|
|
45 |
ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
|
|
|
46 |
boolean cancelledPages = false;
|
|
|
47 |
List<Integer> cancelledPageList = new ArrayList<>();
|
|
|
48 |
|
|
|
49 |
Document document = new Document(PageSize.A4, 28, 28, 34, 34);
|
|
|
50 |
PdfWriter pdfWriter = PdfWriter.getInstance(document, pdfBuffer);
|
|
|
51 |
document.open();
|
|
|
52 |
|
|
|
53 |
for (DocumentData data : docs) {
|
|
|
54 |
if (data.isCancelled()) {
|
|
|
55 |
cancelledPageList.add(1);
|
|
|
56 |
cancelledPages = true;
|
|
|
57 |
} else {
|
|
|
58 |
cancelledPageList.add(0);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
PdfContext ctx = new PdfContext(document, data);
|
|
|
62 |
for (Section section : SECTIONS) {
|
|
|
63 |
section.render(ctx);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
document.newPage();
|
|
|
67 |
|
|
|
68 |
if (data.eWayBill() != null) {
|
|
|
69 |
EWayBillPDF.generateDocument(document, pdfWriter, data.eWayBill());
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
document.close();
|
|
|
74 |
if (cancelledPages) {
|
|
|
75 |
CancelledStamp.apply(pdfBuffer, cancelledPageList);
|
|
|
76 |
}
|
|
|
77 |
pdfBuffer.writeTo(out);
|
|
|
78 |
} catch (DocumentException e) {
|
|
|
79 |
LOGGER.error("Unable to write data to pdf file : ", e);
|
|
|
80 |
} catch (Exception e) {
|
|
|
81 |
e.printStackTrace();
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
}
|