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.bootspring-boot-starter-freemarker${spring-boot-starter-freemarker.version}com.lowagieitext2.0.8com.itextpdfitext-asian5.2.0com.itextpdfitextpdf5.0.6com.itextpdfitext-xtra5.4.5org.xhtmlrenderercore-renderer8.0


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

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部