Subversion Repositories SmartDukaan

Rev

Rev 21977 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.dtrapi.utils;

import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import in.shop2020.dtrapi.Storage.Mongo;

public class PdfGenerator {
        private static final Map<String, String> titles = new HashMap<String, String>();
        static{
                titles.put("bEntityDoc", "bEntityDoc");
                titles.put("gstDoc", "gstDoc");
                titles.put("panDoc", "panDoc");
                titles.put("itrDoc", "itrDoc");
                titles.put("angleDoc1", "angleDoc1");
                titles.put("angleDoc2", "angleDoc2");
                titles.put("angleDoc3", "angleDoc3");
                titles.put("angleDoc4", "angleDoc4");
                titles.put("angleDoc5", "angleDoc5");
                titles.put("ownershipDoc", "ownershipDoc");
                titles.put("chequeCopy", "chequeCopy");
        }
        private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA  , 18, Font.BOLD);
        private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
        public static void generateAndWrite(OutputStream outputStream, int fofoId) throws Exception{
                //String responseString = HttpClientUtil.doGet("localhost:8080", fofoId);
        Document document = new Document();
        document.setMargins(0, 0, 25, 0);
        PdfWriter.getInstance(document, outputStream);
        document.open();
        Paragraph paragraphTitle = new Paragraph("sdfsdfsdfsdf", FONT_TITLE);
        paragraphTitle.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraphTitle);
        
        String jsonString = Mongo.getFofoFormJsonStringByFofoId(fofoId);
        Gson gson = new Gson();
        Map<String, String> titlePathMap = new HashMap<String, String>();
        Type stringStringMap = new TypeToken<Map<String, String>>(){}.getType();
        Map<String, String> map = gson.fromJson(jsonString, stringStringMap);
        System.out.println(map);
        for(String key : map.keySet()){
                String value = map.get(key).toString();
                if(value.startsWith("/hsps-docs/")){
                        titlePathMap.put(titles.get(key), value);
                }
        }
        System.out.println(titlePathMap);
        for(Map.Entry<String, String> entry : titlePathMap.entrySet()){
                Paragraph title = new Paragraph(entry.getKey(), FONT_BOLD);
                document.add(title);
                try{
                        Image img = Image.getInstance(entry.getValue());
                        document.setPageSize(img);
                document.newPage();
                img.setAbsolutePosition(0, 0);
                document.add(img);
                }catch (Exception e) {
                        e.printStackTrace();
                        }
        }
        
        /*PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell();
        ElementList list = XMLWorkerHelper.parseToElementList(responseString, null);
        for (Element element : list) {
            cell.addElement(element);
        }
        table.addCell(cell);
        document.add(table);*/
 
        // step 5
        document.close();
        }
}