1 需要Yoon感到的pom文件依赖 org.apache.pdfbox pdfbox 2.0.1 com.itextpdf itextpdf 5.5.1

com.itextpdf itext-asian 5.2.0

org.freemarker freemarker 2.3.30

org.xhtmlrenderer flying-saucer-pdf 9.0.9 org.xhtmlrenderer flying-saucer-pdf 9.0.9 2 前端掉用生成pdf按钮时: service层方法(vo是参数,里面封装了参数id,是决定打印某条数据,请求和响应就不讲了...) // 代码里的参数封装在常量类里面,这里常量类不展示,具体的值写在//后面 public void printPdfFile(GetInspectInfoVo vo, HttpServletRequest request, HttpServletResponse response) { ByteArrayOutputStream baos = null; OutputStream out = null; try { // 模板中的数据,实际运用从数据库中查询 Map<String, Object> data = new HashMap<>(); GetInspectInfoDto dto = getInspectInfo(vo);// 这里是我自己封装的对象,里面是我自己把pdf信息封装在这个对象里面,模板直接从这里对象里面取值就可以了 data.put(SysCode.PDF.NAME, dto);//键是名字自己定义 baos = PDFTemplateUtil.createPDF(data, SysCode.PDF.FTLNAME);//第二个参数:实地核查情况记录表.ftl // 设置响应消息头,告诉浏览器当前响应是一个下载文件 response.setContentType(SysCode.PDF.CONTENT_TYPE);//application/x-msdownload // 告诉浏览器,当前响应数据要求用户干预保存到文件中,以及文件名是什么 如果文件名有中文,必须URL编码 String fileName = URLEncoder.encode(SysCode.PDF.PDFNAME, SysCode.PDF.CODEUTF_8);//实地核查情况记录表.pdf;UTF-8 response.setHeader(SysCode.PDF.CD, SysCode.PDF.AF + fileName);//Content-Disposition;attachment;filename= out = response.getOutputStream(); baos.writeTo(out); baos.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (baos != null) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } 3 工具类 public class PDFTemplateUtil { /** * 通过模板导出pdf文件 * * @param data 数据 * @param templateFileName 模板文件名 * @throws Exception */ public static ByteArrayOutputStream createPDF(Map<String, Object> data, String templateFileName) throws Exception { // 创建一个FreeMarker实例, 负责管理FreeMarker模板的Configuration实例 Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); // 指定FreeMarker模板文件的位置 cfg.setClassForTemplateLoading(PDFTemplateUtil.class, "/templates"); ITextRenderer renderer = new ITextRenderer(); OutputStream out = new ByteArrayOutputStream(); try { // 设置 css中 的字体样式(暂时仅支持宋体和黑体) 必须,不然中文不显示 // File directory = new File("src/main/resources"); // String courseFile = directory.getCanonicalPath(); renderer.getFontResolver().addFont("/templates/font/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // 设置模板的编码格式 cfg.setEncoding(Locale.CHINA, "UTF-8"); // 获取模板文件 Template template = cfg.getTemplate(templateFileName, "UTF-8"); StringWriter writer = new StringWriter(); // 将数据输出到html中 template.process(data, writer); writer.flush(); String html = writer.toString(); // 把html代码传入渲染器中 renderer.setDocumentFromString(html); // 设置模板中的图片路径 (这里的images在resources目录下) 模板中img标签src路径需要相对路径加图片名 如
 // String url = PDFTemplateUtil.class.getClassLoader().getResource("images").toURI().toString(); // renderer.getSharedContext().setBaseURL(url); renderer.layout(); renderer.createPDF(out, false); renderer.finishPDF(); out.flush(); return (ByteArrayOutputStream) out; } finally { if (out != null) { out.close(); } } } } 4 模板路径在resource下 (resources\templates\实地核查情况记录表.ftl) 字体在resources\templates\font\simsun.ttc 根据需要打印的pdf样子制作模板,模板不会问前端,前端不会就把前端打一顿!!

窗口标题

....记录表

....<#list re.infoList as info>

...名称

${re.supervisorName}

...代表人

${re.fddbr}

行政区划

${re.xzqhName}

...信用代码

${re.tyshxydm}

...号码

${re.llyhm}

...

...

...

...

${info.ccdw}

${info.cclb}

${info.itemName}

${info.hgqkTxt} <#--是 否-->

..人员

${re.deptCheckUser}

...记地址(住所)

${re.address}

检查结果

${re.inspectionResultTxt}

处理意见

${re.dealOpinion}

检查人员签字

<#list re.gcryqzs as signs>

<#--${re.gcryqzs}-->

检查日期

${re.time}

企业法定代表人签字(或加盖企业公章)

// 图片直接使用base64字符串放在这里可以显示的