| 21974 |
ashik.ali |
1 |
package in.shop2020.dtrapi.utils;
|
|
|
2 |
|
| 21977 |
amit.gupta |
3 |
import in.shop2020.dtrapi.Storage.Mongo;
|
|
|
4 |
|
| 21974 |
ashik.ali |
5 |
import java.io.OutputStream;
|
|
|
6 |
import java.lang.reflect.Type;
|
|
|
7 |
import java.util.HashMap;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
|
|
|
10 |
import com.google.gson.Gson;
|
|
|
11 |
import com.google.gson.reflect.TypeToken;
|
|
|
12 |
import com.itextpdf.text.Document;
|
|
|
13 |
import com.itextpdf.text.Element;
|
|
|
14 |
import com.itextpdf.text.Font;
|
|
|
15 |
import com.itextpdf.text.Image;
|
|
|
16 |
import com.itextpdf.text.Paragraph;
|
| 21977 |
amit.gupta |
17 |
import com.itextpdf.text.pdf.PdfContentByte;
|
|
|
18 |
import com.itextpdf.text.pdf.PdfImportedPage;
|
|
|
19 |
import com.itextpdf.text.pdf.PdfPCell;
|
|
|
20 |
import com.itextpdf.text.pdf.PdfPTable;
|
|
|
21 |
import com.itextpdf.text.pdf.PdfReader;
|
| 21974 |
ashik.ali |
22 |
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
23 |
|
|
|
24 |
public class PdfGenerator {
|
|
|
25 |
private static final Map<String, String> titles = new HashMap<String, String>();
|
|
|
26 |
static{
|
| 21977 |
amit.gupta |
27 |
titles.put("bEntityDoc", "Business Document");
|
|
|
28 |
titles.put("gstDoc", "GST Doc");
|
|
|
29 |
titles.put("panDoc", "Pan Doc");
|
|
|
30 |
titles.put("itrDoc", "ITR DOC");
|
|
|
31 |
titles.put("angleDoc1", "Angle Doc1");
|
|
|
32 |
titles.put("angleDoc2", "Angle Doc2");
|
|
|
33 |
titles.put("angleDoc3", "Angle Doc3");
|
|
|
34 |
titles.put("angleDoc4", "Angle Doc4");
|
|
|
35 |
titles.put("angleDoc5", "Angle Doc5");
|
|
|
36 |
titles.put("ownershipDoc", "OwnerShip Doc");
|
|
|
37 |
titles.put("chequeCopy", "Cheque");
|
|
|
38 |
titles.put("insuranceDoc", "Insurance Doc");
|
|
|
39 |
titles.put("loanDoc", "Loan Doc");
|
|
|
40 |
titles.put("sanctionDoc", "Sanction Doc");
|
| 21974 |
ashik.ali |
41 |
}
|
|
|
42 |
private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA , 18, Font.BOLD);
|
|
|
43 |
private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
|
|
|
44 |
public static void generateAndWrite(OutputStream outputStream, int fofoId) throws Exception{
|
|
|
45 |
//String responseString = HttpClientUtil.doGet("localhost:8080", fofoId);
|
|
|
46 |
Document document = new Document();
|
| 21977 |
amit.gupta |
47 |
|
|
|
48 |
PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
|
| 21974 |
ashik.ali |
49 |
document.open();
|
|
|
50 |
|
| 21977 |
amit.gupta |
51 |
PdfContentByte cb = pdfWriter.getDirectContent();
|
|
|
52 |
|
| 21974 |
ashik.ali |
53 |
String jsonString = Mongo.getFofoFormJsonStringByFofoId(fofoId);
|
|
|
54 |
Map<String, String> titlePathMap = new HashMap<String, String>();
|
|
|
55 |
Type stringStringMap = new TypeToken<Map<String, String>>(){}.getType();
|
| 21977 |
amit.gupta |
56 |
Gson gson = new Gson();
|
| 21974 |
ashik.ali |
57 |
Map<String, String> map = gson.fromJson(jsonString, stringStringMap);
|
| 21977 |
amit.gupta |
58 |
|
|
|
59 |
Paragraph paragraphTitle = new Paragraph(map.get("registeredBusinessName"), FONT_TITLE);
|
|
|
60 |
paragraphTitle.setAlignment(Element.ALIGN_CENTER);
|
|
|
61 |
document.add(paragraphTitle);
|
| 21974 |
ashik.ali |
62 |
System.out.println(map);
|
|
|
63 |
for(String key : map.keySet()){
|
|
|
64 |
String value = map.get(key).toString();
|
|
|
65 |
if(value.startsWith("/hsps-docs/")){
|
|
|
66 |
titlePathMap.put(titles.get(key), value);
|
|
|
67 |
}
|
|
|
68 |
}
|
| 21977 |
amit.gupta |
69 |
|
|
|
70 |
PdfPTable table = new PdfPTable(1);
|
|
|
71 |
PdfPCell title;
|
|
|
72 |
PdfPTable innerTable;
|
|
|
73 |
|
|
|
74 |
|
| 21974 |
ashik.ali |
75 |
for(Map.Entry<String, String> entry : titlePathMap.entrySet()){
|
| 21977 |
amit.gupta |
76 |
Image image = null;
|
|
|
77 |
try {
|
|
|
78 |
image = Image.getInstance(entry.getValue());
|
|
|
79 |
} catch (Exception e) {
|
| 21974 |
ashik.ali |
80 |
e.printStackTrace();
|
| 21977 |
amit.gupta |
81 |
PdfReader reader = new PdfReader(entry.getValue());
|
|
|
82 |
for (int i=1; i<=reader.getNumberOfPages(); i++){
|
|
|
83 |
PdfImportedPage page = pdfWriter.getImportedPage(reader, i);
|
|
|
84 |
cb.addTemplate(page, 0, 0);
|
|
|
85 |
document.newPage();
|
|
|
86 |
}
|
|
|
87 |
continue;
|
|
|
88 |
}
|
|
|
89 |
innerTable = new PdfPTable(1);
|
|
|
90 |
float scaler = (Math.min((document.getPageSize().getWidth()) / image.getWidth(), 1)) * 100;
|
|
|
91 |
title = new PdfPCell(new Paragraph(entry.getKey(), FONT_BOLD));
|
|
|
92 |
title.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
93 |
innerTable.addCell(title);
|
|
|
94 |
image.scalePercent(scaler);
|
|
|
95 |
innerTable.addCell(image);
|
|
|
96 |
table.addCell(innerTable);
|
| 21974 |
ashik.ali |
97 |
}
|
| 21977 |
amit.gupta |
98 |
document.add(table);
|
| 21974 |
ashik.ali |
99 |
document.close();
|
|
|
100 |
}
|
|
|
101 |
}
|