springboot通过ITextPDF写入模板并下载

更新时间:2023-07-31 06:17:32 阅读: 评论:0

springboot通过ITextPDF写⼊模板并下载
springboot 通过ITextPDF在java中写⼊PDF模板并导出下载
1、准备:PDF模板,写⼊PDF的字体,maven依赖或jar包。这两个⽂件放在resources⽂件夹下。
<!-- iTextPDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
2.开始,从请求中获得HTTPServletRequest , HTTPServletRespon对象,从服务器获取PDF模板⽂件地址,font字体地址,以及填充模板的map键值对
我的PDF模板是放在resources下的public⽂件夹下,获取地址如下,你们要⽤的话改为⾃⼰地址就好了。拿到地址之后按⾃⼰需要去⽤URLDecoder解析⼀下,有中⽂的话就需要解析,全英⽂不需要,还是解析⼀下⽐较好,哈哈。font地址同上
// 获取服务器PDF模板⽂件
Resource resource = Resource("classpath:/public/PDFTemplate.pdf");
String filePath;
try{
filePath = URLDecoder.URL().getPath(),"utf-8");
}catch(IOException e){
e.printStackTrace();
log.info("⽂件地址URL解析失败!");
return"下载失败!";
}
然后准备map键值对即可,键存放模板表单name值,值存放你想要放的值即可
Map<String, Object> values =new HashMap<String, Object>();
values.put("title","这是标题");
values.put("nameA","张三");
values.put("nameA","李四");
values.put("agreeA","张三同意");
values.put("agreeB","李四同意");
3.调⽤util⽅法即可PDFUtil.writePDFAndDownload(request, respon, filePath, fontPath, fileName, values);具体代码如下
private static BaFont bf;
/**
* Title: writePDFAndDownload
* Description: 对PDF模板写⼊内容并下载,请求参数request,传⼊响应请求respon,pdf模板⽂件地址filePath,
*    写⼊pdf的字体地址fontPath,下载时需要显⽰的⽂件名称fileName,模板表单对应字段键值对values
* Date: 2019年8⽉21⽇
* String
*/
public static String writePDFAndDownload(HttpServletRequest request, HttpServletRespon respon,
String filePath, String fontPath, String fileName, Map<String, Object> values){
// 处理⽂件名转码
String header = Header("Ur-Agent").toUpperCa();
try{
ains("MSIE")|| ains("TRIDENT")|| ains("EDGE")){
// IE下载⽂件名空格变+号问题
fileName = de(fileName,"utf-8");
fileName = place("+","%20");
}el{
fileName =new Bytes(),"ISO8859-1");
}
}catch(UnsupportedEncodingException e){
dha胶囊
e.printStackTrace();
return"⽂件名编码错误!";
}
// 处理响应为下载附件形式
<();
respon.tContentType("multipart/form-data");
respon.tContentType("application/octet-stream; chart=utf-8");
respon.tHeader("Location", fileName);
respon.tHeader("Content-Disposition","attachment; fileName="+ fileName);
respon.tHeader("Cache-Control","max-age=0");
// 设置写⼊PDF的字体
try{
神阙怎么读
bf = ateFont(fontPath, BaFont.IDENTITY_H, BaFont.EMBEDDED);
}catch(DocumentException e){
e.printStackTrace();
return"⽂档字体⾮法";
}catch(IOException e){
e.printStackTrace();
return"⽂档字体⽆法识别";
}
PdfReader pdfReader;
山羊英文
ByteArrayOutputStream baos;
PdfStamper pdfStamper;
try{
OutputStream os = OutputStream();
pdfReader =new PdfReader(filePath);
baos =new ByteArrayOutputStream();
pdfStamper =new PdfStamper(pdfReader, baos);
// 为PDF模板表单字段写⼊对应的值
五月婷婷丁香五月AcroFields fields = AcroFields();
fields.addSubstitutionFont(bf);
for(Entry<String, Object> entry : Set()){
fields.Key(), String.Value()));
}
/
/ 设置PDF为不可编辑状态true
pdfStamper.tAnnotationFlattening(true);
pdfStamper.clo();
// 将⽂件下载
Document doc =new Document();
PdfCopy copy =new PdfCopy(doc, os);
doc.open();
PdfImportedPage page = ImportedPage(new ByteArray()),1);  copy.addPage(page);
// 关闭流
doc.clo();
os.flush();
os.clo();拔苗助长成语故事
}catch(IOException e){
七夕小故事e.printStackTrace();
return"传输失败!";
}catch(DocumentException e){
e.printStackTrace();
千家万户造句
return"获取⽂档失败";
}
q版动漫人物图片return null;
}
⽅法中通过for循环将values中键值对写⼊PDF,键值要和模板字段值对应。

本文发布于:2023-07-31 06:17:32,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1102783.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模板   地址   下载   请求   键值   字体   获取   表单
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图