Subversion Repositories SmartDukaan

Rev

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

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