java导出pdf功能记录
这几天已在做处理导出pdf文件的功能,摸索了几天总算可以了。记录下这几天遇到的问题。
1.网上基本都是基于Itext5和Itext7来处理的。我最终是在Itext5上成功了,itext7应该是模板出问题了,在写的test方法里面测试时候,总是 找不到 pdf文档里面的form表单内容。因为需要使用adobe acrobat reader dc 来制作模板,而我没有这个。一直导致失败。
2.最终成功是参考这个文档:
https://blog.csdn.net/yi2419808933/article/details/52469241
https://blog.csdn.net/yi2419808933/article/details/52469241
3.贴上最终代码
/**
* 导出对账单
* @return
*/
@RequestMapping("/exportSupplierFeeData")
@ResponseBody
@MasterSlaveConfigs(configs={
@MasterSlaveConfig(databaseTag="mdbcarmanage-DataSource",mode= DynamicRoutingDataSource.DataSourceMode.SLAVE)
} )
public AjaxResponse exportSupplierFeeData(HttpServletRequest request, HttpServletResponse response,
ServletOutputStream outputStream,
Integer cityId, Integer supplierId,
Integer status, Integer amountStatus, String settleStartDate,
String settleEndDate, String paymentStartTime, String paymentEndTime, String feeOrderNo){
logger.info(MessageFormat.format("查询司机线上化入参:cityId:%s,supplierId:%s,status:%s," +
"amountStatus:%s,settleStartDate:%s,settleEndDate:%s,paymentStartTime:%s,paymentEndTime:%s",cityId,supplierId,status,amountStatus,settleStartDate,settleEndDate,paymentStartTime,paymentEndTime)); response.setContentType("application/pdf;charset=ISO8859-1");
response.setHeader("Content-disposition", "attachment; filename="+"supplierFeePDF-1.pdf"); SupplierFeeManageDto feeManageDto = new SupplierFeeManageDto();
feeManageDto.setCityId(cityId);
feeManageDto.setSettleStartDate(settleStartDate);
feeManageDto.setSettleEndDate(settleEndDate);
feeManageDto.setAmountStatus(amountStatus);
feeManageDto.setStatus(status);
SupplierFeeManage manage = supplierFeeService.queryByOrderNo(feeOrderNo); String[] titles = { "供应商名称1", "列名t2", "列名3", "列名4", "t5", "t6","t7"};
try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
Document document = new Document(PageSize.A3); // Step 1—Create a Document.
PdfWriter writer;
writer = PdfWriter.getInstance(document, ba);
document.open(); document.setMargins(5, 5, 5, 5);
//H 代表文字版式是横版,相应的 V 代表竖版
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//STSongStd-Light 是字体,在jar 中以property为后缀
//参数一:新建好的字体;参数二:字体大小,参数三:字体样式,多个样式用“|”分隔 Font topfont = new Font(bfChinese,14,Font.BOLD);
//BaseFont bf = BaseFont.createFont("" + "fonts/simsun.ttc,0", BaseFont.IDENTITY_H,
// BaseFont.EMBEDDED); Paragraph blankRow1 = new Paragraph(18f, " ");
blankRow1.setAlignment(Element.ALIGN_CENTER); PdfPTable table1 = new PdfPTable(titles.length); //创建一个表格,参数为一行有几栏
int width1[] = {100,150,150,100,50,50,55};//每栏的宽度
table1.setWidths(width1); //设置宽度 //首行
for(int i=0;i<titles.length;i++){
PdfPCell cell1 = new PdfPCell(new Paragraph(titles[i],topfont));
table1.addCell(cell1);
} //每栏的值
//PdfPCell cell1 = new PdfPCell(new Paragraph(manage.getSupplierId())); /* PdfPTable celltable = new PdfPTable(2); cell1 = new PdfPCell(celltable);
cell1.setRowspan(2);
cell1.setPadding(10);
table1.addCell(cell1);*/ PdfPCell cell1 = new PdfPCell(new Paragraph("5555",topfont));
table1.addCell(cell1);
PdfPCell cell2= new PdfPCell(new Paragraph(manage.getFlowAmount(),topfont));
table1.addCell(cell2); PdfPCell cell3= new PdfPCell(new Paragraph(manage.getSettleStartDate().toString(),topfont));
table1.addCell(cell3);
PdfPCell cell4= new PdfPCell(new Paragraph(manage.getPaymentTime().toString(),topfont));
table1.addCell(cell4);
PdfPCell cell5= new PdfPCell(new Paragraph(manage.getFeeOrderNo(),topfont));
table1.addCell(cell5);
PdfPCell cell6= new PdfPCell(new Paragraph(manage.getSupplierId().toString(),topfont));
table1.addCell(cell6);
PdfPCell cell7= new PdfPCell(new Paragraph(manage.getSupplierName().toString(),topfont));
table1.addCell(cell7); document.add(table1);//将表格加入到document中
// document.add(table2);
document.add(blankRow1);
document.close(); ba.writeTo(outputStream);
outputStream.flush();
outputStream.close(); ba.close(); // 导出pdf注解 } catch (Exception e) {
e.printStackTrace();
} // 模板路径 String templatePath = "/Users/fan/workspace/mp-manage/src/main/webapp/upload/supplierFeePDF-1.pdf"; // 生成的新文件路径
String newPDFPath = "/Users/fan/workspace/mp-manage/src/main/webapp/upload/supplierFeePDF-2.pdf";
/* PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream(); stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
*//*String[] str = {manage.getSupplierName(), DateUtils.formatDate(manage.getSettleStartDate()),DateUtils.formatDate(manage.getSettleEndDate()),
manage.getFlowAmount()};*//*
String[] str = { "123456789", "TOP__ONE", "男", "1991-01-01", "130222111133338888", "河北省保定市" };
java.util.Iterator<String> it = form.getFields().keySet().iterator(); *//*for(int i = 0;i<feeManageList.size();i++){
SupplierFeeManage feeManage = feeManageList.get(i); form.setField(null,feeManage.getSupplierName());
}*//* int i = 0;
while (it.hasNext()) {
String name = it.next().toString();
System.out.println(name);
form.setField(name, str[i++]);
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (IOException e) {
System.out.println(1);
} catch (DocumentException e) {
System.out.println(2);
}
*/
return null;
}
java导出pdf功能记录的更多相关文章
- JAVA导出pdf实例
一.直接导出成PDF Java代码 1. import java.io.FileNotFoundException; 2. import java.io.FileOutputStream; 3. ...
- 导出pdf功能
本程序下载地址: PDF是我们极其常用的文件格式,但对如何生成PDF,个人一直觉得很神秘,其实利用一些公开的PDF库,我们就可以直接生成PDF文件,而不用关注PDF文件的内部细节.我知道的PDF库有如 ...
- Java导出Pdf格式表单
前言 作为开发人员,工作中难免会遇到复杂表单的导出,接下来介绍一种通过Java利用模板便捷导出Pdf表单的方式 模拟需求 需求:按照下面格式导出pdf格式的学生成绩单 准备工作 Excel软件 ...
- Java导出pdf文件数据
提示:导出pdf文件,需要3个jar包iText-2.1.5.jar,iTextAsian.jar,iText-rtf-2.1.4.jar. public boolean outputPdfJhsy( ...
- java导出pdf
//导出 public void ScoringAnnouncementdownLoad() throws MalformedURLException, IOException, D ...
- 一次java导出pdf的经历
近期由于工作需要,需要将html代码导入到pdf中,经过了几种pdf的方案对比后发现IText是最简单和便捷的一种方式,于是乎采用了Itext. PDF生成 第一步:导入Maven依赖 <!-- ...
- C# 导出CSV功能记录下
异常问题1: 如 机构编号 导出城CSV后,前面的四个0000不显示了, 解决办法 输出格式变为 =" 异常问题2: PPMABAT01:/MABATAPS/usr/ma_batas > ...
- java根据模板导出PDF详细教程
原文:https://blog.csdn.net/pengyufight/article/details/75305128 题记:由于业务的需要,需要根据模板定制pdf文档,经测试根据模板导出word ...
- ASP.NET C#根据HTML页面导出PDF
在启明星采购系统里,新增了导出PDF功能.整个功能使用了第三方软件 wkhtmltopdf(下载) 官网 https://wkhtmltopdf.org/ 提供有更多版本下载 他可以把HTML页面转换 ...
随机推荐
- js事件2-事件兼容问题
对于不同的浏览器,事件响应会有一定的不同,所以我们为了更好的用户效果,必须要考虑好事件兼容性问题. 为了兼容不同的浏览器,我们可以自己编写一个事件对象,通过它的事件添加函数和删除函数来给元素添加/删除 ...
- 201671010425邱世妍 团队评审&课程总结
实验十四 团队项目评审&课程学习总结 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cn ...
- 利用Java反射机制优化简单工厂设计模式
之前项目有个需求,审批流程的时候要根据配置发送信息:发送短信.发送邮件.当时看到这个就想到要用工厂模式,为什么要用工厂模式呢?用工厂模式进行大型项目的开发,可以很好的进行项目并行开发.就是一个程序员和 ...
- spark的RDDAPI总结
下面是RDD的基础操作API介绍: 操作类型 函数名 作用 转化操作 map() 参数是函数,函数应用于RDD每一个元素,返回值是新的RDD flatMap() 参数是函数,函数应用于RDD每一个元素 ...
- Ajax无法访问回调函数seccess问题
1,后台返回的数据是标准json格式,前端dataType也是josn, 2,没有跨域访问, 但是一直只执行error方法, 原因出在: 应设置为button按钮,指明类型为button
- ios app ipv6 审核总结
AAAA记录(AAAA record)是用来将域名解析到IPv6地址的DNS记录.用户可以将一个域名解析到IPv6地址上,也可以将子域名解析到IPv6地址上. 1)$ dig +nocmd +nost ...
- hdu1010-Tempter of the Bone-(dfs+奇偶剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 翻译:有只狗被困了,S是起点,D是门,W是墙不能走,‘ . ’是可以走的路,走一次就会在1秒内坍塌,也就是 ...
- 【DP】【期望】$P1850$换教室
链接 题目描述 有 \(2n\) 节课程安排在$ n$ 个时间段上.在第 \(i\)(\(1 \leq i \leq n\))个时间段上,两节内容相同的课程同时在不同的地点进行,其中,牛牛预先被安排在 ...
- 大数定律(Law of Large Numbers)
大数定律:每次从总体中随机抽取1个样本,这样抽取很多次后,样本的均值会趋近于总体的期望.也可以理解为:从总体中抽取容量为n的样本,样本容量n越大,样本的均值越趋近于总体的期望.当样本容量极大时,样本均 ...
- nginx之系统参数优化
系统参数优化 默认的Linux内核参数考虑的是最通用场景,不符合用于支持高并发访问的Web服务器的定义,根据业务特点来进行调整,当Nginx作为静态web内容服务器.反向代理或者提供压缩服务器的服务器 ...