Subversion Repositories SmartDukaan

Rev

Rev 21977 | Rev 21983 | Go to most recent revision | 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>();
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++){
21982 amit.gupta 83
        			try {
21977 amit.gupta 84
        			PdfImportedPage page = pdfWriter.getImportedPage(reader, i);
85
        			cb.addTemplate(page, 0, 0);
86
        			document.newPage();
21982 amit.gupta 87
        			}catch (Exception ex) {
88
        				ex.printStackTrace();
89
        				document.add(new Paragraph(entry.getKey()  + " is missing of password encrypted."));
90
        			}
21977 amit.gupta 91
        		}
92
        		continue;
93
        	}
94
        	innerTable = new PdfPTable(1);
95
        	float scaler = (Math.min((document.getPageSize().getWidth()) / image.getWidth(), 1)) * 100;
96
        	title = new PdfPCell(new Paragraph(entry.getKey(), FONT_BOLD));
97
        	title.setHorizontalAlignment(Element.ALIGN_CENTER);
98
        	innerTable.addCell(title);
99
        	image.scalePercent(scaler);
100
        	innerTable.addCell(image);
101
        	table.addCell(innerTable);
21974 ashik.ali 102
        }
21977 amit.gupta 103
        document.add(table);
21974 ashik.ali 104
        document.close();
105
	}
106
}