java生成PDF,并下载到本地
1、首先要写一个PDF工具类,以及相关工具
2、PDF所需jar包
iText是一种生成PDF报表的Java组件
freemarker是基于模板来生成文本输出
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
3、需要使用Adobe Acrobat pro软件把要生成的模板转换为PDF格式
打开Adobe Acrobat pro,打开模板,选择 |—— 准备表单 ,它会自动检测并命名表单域,然后保存为pdf格式即可
PDF工具类
public class PDFTemplet {
private String templatePdfPath;
private String targetPdfpath;
private ServiceOrder order ;
public PDFTemplet() {
}
public void PDFTemplet(File file,String basePath)thows Exception{
/*模板路径*/
PdfReader reader = new PdfReader(templatePdfPath);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
/* 读取*/
PdfStamper pdfStamper= new PdfStamper(reader,bos);
/*使用中文字体*/
BaseFont baseFont=BaseFont.createFont(basePath+"WEB-INF/static/SIMHEI.TTF",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
ArrayList<BaseFont> fontList=new ArrayList<>();
fontList.add(baseFont);
AcroFields s=pdfStamper.getAcroFields();
s.setSubstitutionFonts(fontList);
/*需要注意的是 setField的name和命名的表单域名字要一致*/
s.setField("enterpriseName",order.getEnerpriseName());
s.setField("incubatorName",order.getIncubatorName());
s.setField("recommend","");//孵化器推荐
s.setField("contacts",order.getContacts());
s.setField("phone",order.getPhone());
s.setField("email",order.getEmail());
s.setField("category ","");//服务类别
s.setField("demand",order.getDemand());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
String createTime = formatter.format(order.getCreateTime());
String updateTime = formatter.format(order.getUpdateTime());
s.setField("createTime",createTime);
s.setField("updateTime", updateTime);
ps.setFormFlattenning(true);
ps.close();
FileOutputStream fileSteam =new FileOutPutStream(file);
fos.write(bos.toByteArray);
fos.close();}
}
调用方法
@RequestMapping(value ="downloadPdf", method = RequestMethod.GET)
public String downloadPDF(@PathVariable("id") Integer id,HttpServletRequest request) throws Exception {
ServiceOrder serviceOrder = serviceOrderService.getById(id);
PDFTemplet pdfTT = new PDFTemplet();
pdfTT.setOrder(serviceOrder);
String basePath = request.getSession().getServletContext().getRealPath("/");
String template = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/static/excel/confirmation.pdf";
pdfTT.setTemplatePdfPath(template);
pdfTT.setTargetPdfpath("D:/企业服务确认单.pdf");
pdfTT.setOrder(serviceOrder);
File file = new File("D:/企业服务确认单.pdf");
file.createNewFile();
pdfTT.templetTicket(file,basePath);
return "/master/serviceOrder/orderList";
}
java生成PDF,并下载到本地的更多相关文章
- java生成pdf
介绍 本篇博客主要是为了介绍如何使用:flying-saucer+itext+freemark实现导出复杂点的pdf文件. 思路 先把pdf的内容以html形式准备好 使用freemarker将htm ...
- Java 生成pdf表格文档
最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...
- Java 使用itext生成pdf以及下载
使用方法: 1.需要两个jar包: iText-5.0.6.jar //必须使用该版本,否则缺少相关的方法 TextAsian.jar //是为了文档中正常显示中文所必须引用的包 TextAsi ...
- 电子凭证 : Java 生成 Pdf
来源:蛙牛, my.oschina.net/lujianing/blog/894365 如有好文章投稿,请点击 → 这里了解详情 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中 ...
- JAVA 生成PDF报表()
许多应用程序都要求动态生成 PDF 文档.这些应用程序涵盖从生成客户对帐单并通过电子邮件交付的银行到购买特定的图书章节并以 PDF 格式接收这些图书章节的读者.这个列表不胜枚举.在本文中,我们将使用 ...
- Java生成PDF报表
一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iText--用于生成PDF文档的一个Java类库.废话不多说,进入正题. 二.iText简介 iText是著名的开放 ...
- java生成PDF文件(itext)
itextpdf-5.4.3.jar下载地址: http://www.kuaipan.cn/file/id_58980483773788178.htm 导入itextpdf-5.4.3.jar ToP ...
- Java生成PDF文件(转)
原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...
- [itext]Java生成PDF文件
一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...
随机推荐
- July 16th 2017 Week 29th Sunday
Opportunities are like sunrises, if you wait too long, you miss them. 机会如同日出,等得太久就会错过. Indecision is ...
- BUG Review:关于getting 'android:xxx' attribute: attribute is not a string value的问题及解决方法
我们在使用Android Studio开发完应用程序后,都要将打好的apk安装包上传到各大应用市场,但是有时候上传时应用市场会出现提交的安装包不能通过应用市场的aapt解析而被打回的情况. 他们使用a ...
- How to update BOL entity property value via ABAP code
Suppose I have one product with ID I042416 which could be found in CRM WebClient UI: I would like to ...
- Event Driven Architecture
在微服务中使用领域事件 稍微回想一下计算机硬件的工作原理我们便不难发现,整个计算机的工作过程其实就是一个对事件的处理过程.当你点击鼠标.敲击键盘或者插上U盘时,计算机便以中断的形式处理各种外部事件 ...
- 记录linux查询命令的一个网站
http://man.linuxde.net/ 另外下面是对常用命令的总结 https://www.cnblogs.com/soyxiaobi/p/9717483.html
- poj1322 Chocolate 【 概率DP 】
题目链接:poj1322 Chocolate [概率DP ] 题意:袋中有C种颜色巧克力,每次从其中拿出一块放桌上,如果桌上有两块相同颜色巧克力则吃掉,问取出N块巧克力后,求桌上正好剩下M块巧克力的概 ...
- 勒让德定理---阶乘中素因子p的指数
- hdu1113 Word Amalgamation(详解--map和string的运用)
版权声明:本文为博主原创文章.未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/35338617 转载请注明出 ...
- MS17-010复现
很早之前做的了,今天整理看到了,正好腾到blog上. ########################分割线############################## MS-17-010 攻击者向 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】
传送门:http://codeforces.com/contest/1093/problem/D D. Beautiful Graph time limit per test 2 seconds me ...