freemark生成pdf
freemark生成pdf
字体库 simsun.ttc 解决中文问题
/*** 生成pdf* @param params* @param templPath* @param ftlName* @param htmlPath* @param pdfPath* @param fontPath* @return*/public String processPdf(Map params, String templPath, String ftlName, String htmlPath,String pdfPath, String fontPath) {// 得到模板地址try {configuration.setDirectoryForTemplateLoading(new File(templPath));Template temp = configuration.getTemplate(ftlName);File htmlFile = new File(htmlPath);if (!htmlFile.exists()) {htmlFile.createNewFile();}Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(htmlPath)), "utf-8"));temp.process(params, out);out.flush();String url = htmlFile.toURI().toURL().toString();OutputStream os = new FileOutputStream(pdfPath);ITextRenderer renderer = new ITextRenderer();renderer.setDocument(url);// 解决中文问题ITextFontResolver fontResolver = renderer.getFontResolver();fontResolver.addFont(fontPath + "simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);renderer.layout();renderer.createPDF(os);os.close();} catch (Exception e) {e.printStackTrace();return "";}return pdfPath;}
org.springframework.boot spring-boot-starter-freemarker ${spring-boot-starter-freemarker.version} com.lowagie itext 2.0.8 com.itextpdf itext-asian 5.2.0 com.itextpdf itextpdf 5.0.6 com.itextpdf itext-xtra 5.4.5 org.xhtmlrenderer core-renderer 8.0

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
