Subversion Repositories SmartDukaan

Rev

Rev 21983 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21983 Rev 22117
Line 21... Line 21...
21
import com.itextpdf.text.pdf.PdfReader;
21
import com.itextpdf.text.pdf.PdfReader;
22
import com.itextpdf.text.pdf.PdfWriter;
22
import com.itextpdf.text.pdf.PdfWriter;
23
 
23
 
24
public class PdfGenerator {
24
public class PdfGenerator {
25
	private static final Map<String, String> titles = new HashMap<String, String>();
25
	private static final Map<String, String> titles = new HashMap<String, String>();
26
	static{
26
	static {
27
		titles.put("bEntityDoc", "Business Document");
27
		titles.put("bEntityDoc", "Business Document");
28
		titles.put("gstDoc", "GST Doc");
28
		titles.put("gstDoc", "GST Doc");
29
		titles.put("panDoc", "Pan Doc");
29
		titles.put("panDoc", "Pan Doc");
30
		titles.put("itrDoc", "ITR DOC");
30
		titles.put("itrDoc", "ITR DOC");
31
		titles.put("angleDoc1", "Angle Doc1");
31
		titles.put("angleDoc1", "Angle Doc1");
Line 37... Line 37...
37
		titles.put("chequeCopy", "Cheque");
37
		titles.put("chequeCopy", "Cheque");
38
		titles.put("insuranceDoc", "Insurance Doc");
38
		titles.put("insuranceDoc", "Insurance Doc");
39
		titles.put("loanDoc", "Loan Doc");
39
		titles.put("loanDoc", "Loan Doc");
40
		titles.put("sanctionDoc", "Sanction Doc");
40
		titles.put("sanctionDoc", "Sanction Doc");
41
	}
41
	}
42
	private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA  , 18, Font.BOLD);
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);
43
	private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
-
 
44
 
44
	public static void generateAndWrite(OutputStream outputStream, int fofoId) throws Exception{
45
	public static void generateAndWrite(OutputStream outputStream, int fofoId) throws Exception {
45
		//String responseString = HttpClientUtil.doGet("localhost:8080", fofoId);
46
		// String responseString = HttpClientUtil.doGet("localhost:8080",
-
 
47
		// fofoId);
46
        Document document = new Document();
48
		Document document = new Document();
47
        
49
 
48
        PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
50
		PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
49
        document.open();
51
		document.open();
50
        
52
 
51
        PdfContentByte cb = pdfWriter.getDirectContent();
53
		PdfContentByte cb = pdfWriter.getDirectContent();
52
        
54
 
53
        String jsonString = Mongo.getFofoFormJsonStringByFofoId(fofoId);
55
		String jsonString = Mongo.getFofoFormJsonStringByFofoId(fofoId);
54
        Map<String, String> titlePathMap = new HashMap<String, String>();
56
		Map<String, String> titlePathMap = new HashMap<String, String>();
55
        Type stringStringMap = new TypeToken<Map<String, String>>(){}.getType();
57
		Type stringStringMap = new TypeToken<Map<String, String>>() {
-
 
58
		}.getType();
56
        Gson gson = new Gson();
59
		Gson gson = new Gson();
57
        Map<String, String> map = gson.fromJson(jsonString, stringStringMap);
60
		Map<String, String> map = gson.fromJson(jsonString, stringStringMap);
58
 
61
 
59
        Paragraph paragraphTitle = new Paragraph(map.get("registeredBusinessName"), FONT_TITLE);
62
		Paragraph paragraphTitle = new Paragraph(map.get("registeredBusinessName"), FONT_TITLE);
60
        paragraphTitle.setAlignment(Element.ALIGN_CENTER);
63
		paragraphTitle.setAlignment(Element.ALIGN_CENTER);
61
        document.add(paragraphTitle);
64
		document.add(paragraphTitle);
62
        System.out.println(map);
65
		System.out.println(map);
63
        for(String key : map.keySet()){
66
		for (String key : map.keySet()) {
64
        	String value = map.get(key).toString();
67
			String value = map.get(key).toString();
65
        	if(value.startsWith("/hsps-docs/")){
68
			if (value.startsWith("/hsps-docs/")) {
66
        		titlePathMap.put(titles.get(key), value);
69
				titlePathMap.put(titles.get(key), value);
67
        	}
70
			}
68
        }
71
		}
69
        
72
 
70
        PdfPTable table = new PdfPTable(1);
73
		PdfPTable table = new PdfPTable(1);
71
        PdfPCell title;
74
		PdfPCell title;
72
        PdfPTable innerTable;
75
		PdfPTable innerTable;
73
 
-
 
74
 
76
 
75
        for(Map.Entry<String, String> entry : titlePathMap.entrySet()){
77
		for (Map.Entry<String, String> entry : titlePathMap.entrySet()) {
76
        	Image image = null;
78
			Image image = null;
77
        	try {
79
			try {
78
        		image = Image.getInstance(entry.getValue());
80
				image = Image.getInstance(entry.getValue());
79
        	} catch (Exception e) {
81
			} catch (Exception e) {
80
        		e.printStackTrace();
82
				e.printStackTrace();
81
        		PdfReader reader = new PdfReader(entry.getValue());
83
				PdfReader reader = new PdfReader(entry.getValue());
-
 
84
				try {
82
        		for (int i=1; i<=reader.getNumberOfPages(); i++){
85
					for (int i = 1; i <= reader.getNumberOfPages(); i++) {
83
        			try {
-
 
84
        			PdfImportedPage page = pdfWriter.getImportedPage(reader, i);
86
						PdfImportedPage page = pdfWriter.getImportedPage(reader, i);
85
        			cb.addTemplate(page, 0, 0);
87
						cb.addTemplate(page, 0, 0);
86
        			document.newPage();
88
						document.newPage();
-
 
89
					}
87
        			}catch (Exception ex) {
90
				} catch (Exception ex) {
88
        				ex.printStackTrace();
91
					ex.printStackTrace();
89
        				document.add(new Paragraph(entry.getKey()  + " is missing or password encrypted."));
92
					document.add(new Paragraph(entry.getKey() + " is missing or password encrypted."));
90
        			}
93
				}
91
        		}
-
 
92
        		continue;
94
				continue;
93
        	}
95
			}
94
        	innerTable = new PdfPTable(1);
96
			innerTable = new PdfPTable(1);
95
        	float scaler = (Math.min((document.getPageSize().getWidth()) / image.getWidth(), 1)) * 100;
97
			float scaler = (Math.min((document.getPageSize().getWidth()) / image.getWidth(), 1)) * 100;
96
        	title = new PdfPCell(new Paragraph(entry.getKey(), FONT_BOLD));
98
			title = new PdfPCell(new Paragraph(entry.getKey(), FONT_BOLD));
97
        	title.setHorizontalAlignment(Element.ALIGN_CENTER);
99
			title.setHorizontalAlignment(Element.ALIGN_CENTER);
98
        	innerTable.addCell(title);
100
			innerTable.addCell(title);
99
        	image.scalePercent(scaler);
101
			image.scalePercent(scaler);
100
        	innerTable.addCell(image);
102
			innerTable.addCell(image);
101
        	table.addCell(innerTable);
103
			table.addCell(innerTable);
102
        }
104
		}
103
        document.add(table);
105
		document.add(table);
104
        document.close();
106
		document.close();
105
	}
107
	}
106
}
108
}