pdf导出功能
来源:互联网 发布:下载青岛网络干部app 编辑:程序博客网 时间:2024/06/05 17:42
依赖包itextpdf:5.5.6
package com.changfu.common.util;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Image;import com.itextpdf.text.Rectangle;import com.itextpdf.text.pdf.*;import lombok.extern.slf4j.Slf4j;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.util.ResourceUtils;import java.io.*;import java.util.Map;/** * PDF生成工具类 * Created by J.W on 2017/4/17 0017. */@Slf4jpublic class PDFUtil { public static final String FORMAT = ".pdf"; private static final String DEFAULT_WINDOWS_PATH = "D:\\data\\contract"; private static final Boolean IS_WINDOWS_OS = System.getProperty("os.name").startsWith("Windows"); /** * 生成pdf * @param targetPath 目标路径 * @param targetName 目标名称(不要带后缀名) * @param fieldsMap 表单填充数据集合 * @param sealsMap 图片填充集合 例: Key:"partyASeal" value:"http://xxxx.png" */ public static File createPDF(String templateName, String targetPath, String targetName, Map<String, String> fieldsMap, Map<String, String> sealsMap) throws IOException, DocumentException { PdfReader reader = null; PdfStamper stamper = null; InputStream is = null; OutputStream output = null; try { is = PDFUtil.class.getClassLoader().getResourceAsStream(templateName); log.info("获取模板字节流[{}]", is);// File templateFile = ResourceUtils.getFile(templateName);// inputStream = new FileInputStream(templateFile); String targetFilePath = null; if(IS_WINDOWS_OS) { targetFilePath = DEFAULT_WINDOWS_PATH + File.separator + targetName + FORMAT; } else { targetFilePath = targetPath + File.separator + targetName + FORMAT; } reader = new PdfReader(is); File targetFile = new File(targetFilePath); if(!targetFile.getParentFile().isDirectory()) { log.info("创建合同文件[{}]", targetFile.getAbsolutePath()); targetFile.getParentFile().mkdirs(); } if(!targetFile.exists()) { log.info("创建合同文件[{}]", targetFile.getAbsolutePath()); targetFile.createNewFile(); } output = new FileOutputStream(targetFile); stamper = new PdfStamper(reader, output); AcroFields acroFields = stamper.getAcroFields(); //给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示 BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);// BaseFont bf = null;// if(IS_WINDOWS_OS) {// bf = BaseFont.createFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);// } else {// bf = BaseFont.createFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);// } acroFields.addSubstitutionFont(bf); // 开始填充表单 for(Map.Entry<String, String> entry : fieldsMap.entrySet()) { if(entry.getValue() == null) { continue; } acroFields.setField(entry.getKey(), entry.getValue()); } // 填充图片 for(Map.Entry<String, String> entry : sealsMap.entrySet()) { if(entry.getValue() == null) { continue; } int pageNo = acroFields.getFieldPositions(entry.getKey()).get(0).page; Rectangle rectangle = acroFields.getFieldPositions(entry.getKey()).get(0).position; float x = rectangle.getLeft(); float y = rectangle.getBottom(); // 读图片 Image image = Image.getInstance(entry.getValue()); // 获取操作的页面 PdfContentByte under = stamper.getOverContent(pageNo); // 根据域的大小缩放图片 image.scaleToFit(rectangle.getWidth(), rectangle.getHeight()); // 添加图片 image.setAbsolutePosition(x, y); under.addImage(image); } stamper.setFormFlattening(true); return targetFile; } catch (Exception e) { log.error("生成pdf失败 templateName={}, targetName={}", templateName, targetName, e); throw e; } finally { try { stamper.close(); } catch (Exception e) { stamper = null; } try { reader.close(); } catch (Exception e) { reader = null; } try { output.close(); } catch (Exception e) { output = null; } try { is.close(); } catch (Exception e) { is = null; } } }}
@RequestMapping("downloadContract/{id}") @RequiresPermissions("tcTransaction:index") @ResponseBody public String downloadContract(Model model, @PathVariable("id") Long id) { return tcContractService.downloadContract(id); }
阅读全文
0 0
- pdf导出功能
- MyReport增加导出PDF功能
- Flex:为MyReport增加导出PDF功能
- Flex:为MyReport增加导出PDF功能
- 使用Itext 进行PDf导出功能
- 【Qt】Qt 导出 PDF,Android和Qt截图功能实现
- Java 实现 FusionCharts 图表导出图片或PDF文件功能
- FusionCharts fcexporter.jar图表导出图片或pdf功能
- 导出pdf
- 导出pdf
- 导出pdf
- 水晶报表导出pdf功能(完整的代码C#版)
- 水晶报表导出为pdf, word , excel 格式(已经测试过,实现了该功能)
- 利用itext操作pdf从数据库导出大量数据--功能汇总(一)
- jquery datatables 图标的导出功能(excel pdf之类)文件
- java iText 的jar导出pdf格式,禁止浏览器预览,直接下载功能
- JQuery Plugin-TableExport实现前端表格的导出功能(已解决表格pdf导出中文格式问题)
- JQuery Plugin-TableExport实现前端表格的导出功能(已解决表格pdf导出中文格式问题)
- Redis系列-5.列表(List)结构
- NoSql简介(-)
- lua学习笔记_metatable,__index,__newindex
- thinkphp 实现查询重复的值,
- 拦截器中,request中getReader()和getInputStream()只能调用一次,构建可重复读取inputStream的request.
- pdf导出功能
- ansible 效率优化
- 51nod 1264 线段相交
- linux(centos)服务器部署web项目
- spring文件上传
- netty实现TimeServer服务
- 无限轮播加小圆点(从网络获取图片)
- Activiti入门之环境搭建
- 前端面试笔试题