Subversion Repositories SmartDukaan

Rev

Rev 22117 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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>();
22117 amit.gupta 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
	}
22117 amit.gupta 42
	private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
21974 ashik.ali 43
	private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
21977 amit.gupta 44
 
22117 amit.gupta 45
	public static void generateAndWrite(OutputStream outputStream, int fofoId) throws Exception {
46
		// String responseString = HttpClientUtil.doGet("localhost:8080",
47
		// fofoId);
48
		Document document = new Document();
21977 amit.gupta 49
 
22117 amit.gupta 50
		PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
51
		document.open();
21977 amit.gupta 52
 
22117 amit.gupta 53
		PdfContentByte cb = pdfWriter.getDirectContent();
54
 
55
		String jsonString = Mongo.getFofoFormJsonStringByFofoId(fofoId);
56
		Map<String, String> titlePathMap = new HashMap<String, String>();
57
		Type stringStringMap = new TypeToken<Map<String, String>>() {
58
		}.getType();
59
		Gson gson = new Gson();
60
		Map<String, String> map = gson.fromJson(jsonString, stringStringMap);
61
 
62
		Paragraph paragraphTitle = new Paragraph(map.get("registeredBusinessName"), FONT_TITLE);
63
		paragraphTitle.setAlignment(Element.ALIGN_CENTER);
64
		document.add(paragraphTitle);
65
		System.out.println(map);
66
		for (String key : map.keySet()) {
67
			String value = map.get(key).toString();
68
			if (value.startsWith("/hsps-docs/")) {
69
				titlePathMap.put(titles.get(key), value);
70
			}
71
		}
72
 
73
		PdfPTable table = new PdfPTable(1);
74
		PdfPCell title;
75
		PdfPTable innerTable;
76
 
77
		for (Map.Entry<String, String> entry : titlePathMap.entrySet()) {
78
			Image image = null;
79
			try {
80
				image = Image.getInstance(entry.getValue());
81
			} catch (Exception e) {
82
				e.printStackTrace();
83
				try {
22118 amit.gupta 84
					PdfReader reader = new PdfReader(entry.getValue());
22117 amit.gupta 85
					for (int i = 1; i <= reader.getNumberOfPages(); i++) {
86
						PdfImportedPage page = pdfWriter.getImportedPage(reader, i);
87
						cb.addTemplate(page, 0, 0);
88
						document.newPage();
89
					}
90
				} catch (Exception ex) {
91
					ex.printStackTrace();
92
					document.add(new Paragraph(entry.getKey() + " is missing or password encrypted."));
93
				}
94
				continue;
95
			}
96
			innerTable = new PdfPTable(1);
97
			float scaler = (Math.min((document.getPageSize().getWidth()) / image.getWidth(), 1)) * 100;
98
			title = new PdfPCell(new Paragraph(entry.getKey(), FONT_BOLD));
99
			title.setHorizontalAlignment(Element.ALIGN_CENTER);
100
			innerTable.addCell(title);
101
			image.scalePercent(scaler);
102
			innerTable.addCell(image);
103
			table.addCell(innerTable);
104
		}
105
		document.add(table);
106
		document.close();
21974 ashik.ali 107
	}
108
}