Subversion Repositories SmartDukaan

Rev

Rev 21974 | Rev 21982 | 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 in.shop2020.dtrapi.Storage.Mongo;

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.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfGenerator {
        private static final Map<String, String> titles = new HashMap<String, String>();
        static{
                titles.put("bEntityDoc", "Business Document");
                titles.put("gstDoc", "GST Doc");
                titles.put("panDoc", "Pan Doc");
                titles.put("itrDoc", "ITR DOC");
                titles.put("angleDoc1", "Angle Doc1");
                titles.put("angleDoc2", "Angle Doc2");
                titles.put("angleDoc3", "Angle Doc3");
                titles.put("angleDoc4", "Angle Doc4");
                titles.put("angleDoc5", "Angle Doc5");
                titles.put("ownershipDoc", "OwnerShip Doc");
                titles.put("chequeCopy", "Cheque");
                titles.put("insuranceDoc", "Insurance Doc");
                titles.put("loanDoc", "Loan Doc");
                titles.put("sanctionDoc", "Sanction Doc");
        }
        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();
        
        PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
        document.open();
        
        PdfContentByte cb = pdfWriter.getDirectContent();
        
        String jsonString = Mongo.getFofoFormJsonStringByFofoId(fofoId);
        Map<String, String> titlePathMap = new HashMap<String, String>();
        Type stringStringMap = new TypeToken<Map<String, String>>(){}.getType();
        Gson gson = new Gson();
        Map<String, String> map = gson.fromJson(jsonString, stringStringMap);

        Paragraph paragraphTitle = new Paragraph(map.get("registeredBusinessName"), FONT_TITLE);
        paragraphTitle.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraphTitle);
        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);
                }
        }
        
        PdfPTable table = new PdfPTable(1);
        PdfPCell title;
        PdfPTable innerTable;


        for(Map.Entry<String, String> entry : titlePathMap.entrySet()){
                Image image = null;
                try {
                        image = Image.getInstance(entry.getValue());
                } catch (Exception e) {
                        e.printStackTrace();
                        PdfReader reader = new PdfReader(entry.getValue());
                        for (int i=1; i<=reader.getNumberOfPages(); i++){
                                PdfImportedPage page = pdfWriter.getImportedPage(reader, i);
                                cb.addTemplate(page, 0, 0);
                                document.newPage();
                        }
                        continue;
                }
                innerTable = new PdfPTable(1);
                float scaler = (Math.min((document.getPageSize().getWidth()) / image.getWidth(), 1)) * 100;
                title = new PdfPCell(new Paragraph(entry.getKey(), FONT_BOLD));
                title.setHorizontalAlignment(Element.ALIGN_CENTER);
                innerTable.addCell(title);
                image.scalePercent(scaler);
                innerTable.addCell(image);
                table.addCell(innerTable);
        }
        document.add(table);
        document.close();
        }
}