/*     */ package com.picc.grab.util;
/*     */ 
/*     */ import com.picc.grab.common.CommonProperties;
/*     */ import freemarker.template.Configuration;
/*     */ import freemarker.template.Template;
/*     */ import java.io.File;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.StringWriter;
/*     */ import java.io.Writer;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import org.xhtmlrenderer.pdf.ITextFontResolver;
/*     */ import org.xhtmlrenderer.pdf.ITextRenderer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FreemarkerUtil
/*     */ {
/*  22 */   private static Configuration freemarkerCfg = null;
/*     */   
/*  24 */   private static final String TEMPLATE_PATH = CommonProperties.PATH + "templates/pdf/";
/*     */ 
/*     */   
/*     */   static {
/*     */     try {
/*  29 */       freemarkerCfg = new Configuration();
/*  30 */       freemarkerCfg.setDefaultEncoding("utf-8");
/*  31 */       freemarkerCfg.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
/*  32 */     } catch (Exception e) {
/*  33 */       e.printStackTrace();
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String createPdf(Map<String, String> data, String carbjid) throws Exception {
/*  45 */     String ftlName = "scbjd2.ftl";
/*  46 */     String content = freeMarkerRender(data, ftlName);
/*  47 */     if (StringUtils.isBlank(content)) {
/*  48 */       throw new Exception();
/*     */     }
/*  50 */     String relativePdfPath = "bjdpdf/";
/*  51 */     String pdfPath = CommonProperties.PATH + relativePdfPath;
/*  52 */     File outPath = new File(pdfPath);
/*  53 */     if (!outPath.exists()) {
/*  54 */       outPath.mkdirs();
/*     */     }
/*  56 */     pdfPath = pdfPath + carbjid + ".pdf";
/*  57 */     relativePdfPath = relativePdfPath + carbjid + ".pdf";
/*     */     
/*  59 */     ITextRenderer render = new ITextRenderer();
/*  60 */     ITextFontResolver fontResolver = render.getFontResolver();
/*     */     
/*  62 */     fontResolver.addFont(TEMPLATE_PATH + "simsun.ttc", "Identity-H", true);
/*     */     
/*  64 */     render.setDocumentFromString(content);
/*     */ 
/*     */     
/*  67 */     render.layout();
/*  68 */     render.createPDF(new FileOutputStream(pdfPath));
/*  69 */     return relativePdfPath;
/*     */   }
/*     */   
/*     */   private static String getCurrentOperatingSystem() {
/*  73 */     String os = System.getProperty("os.name").toLowerCase();
/*  74 */     System.out.println("---------当前操作系统是-----------" + os);
/*  75 */     return os;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static String freeMarkerRender(Map<String, String> data, String htmlTmp) {
/*  82 */     Writer out = new StringWriter();
/*     */     
/*     */     try {
/*  85 */       Template template = freemarkerCfg.getTemplate(htmlTmp);
/*  86 */       template.setEncoding("UTF-8");
/*     */       
/*  88 */       template.process(data, out);
/*  89 */       out.flush();
/*  90 */       return out.toString();
/*  91 */     } catch (Exception e) {
/*  92 */       e.printStackTrace();
/*     */     } finally {
/*     */       try {
/*  95 */         out.close();
/*  96 */       } catch (IOException ex) {
/*  97 */         ex.printStackTrace();
/*     */       } 
/*     */     } 
/* 100 */     return null;
/*     */   }
/*     */ }


/* Location:              C:\Users\Administrator\Desktop\ROOT\WEB-INF\classes\!\com\picc\gra\\util\FreemarkerUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */