Java根据模板导出PDF文件

更新时间:2023-07-31 06:03:05 阅读: 评论:0

Java根据模板导出PDF⽂件
Java根据模板导出PDF⽂件
在⽇常使⽤中,会涉及到将数据库中的数据进⾏计算处理,并将数据写在PDF模板
1 封装数据
代码是伪代码,不能导⼊直接运⾏,简单的看⼀下很容理解
public Map<String, Map<String, Object>>getScorePackage(String type, String urId){
//查询出多条数据,使⽤map,将数据封装起来,便于批量下载PDF
Map<String, Map<String, Object>> map =new HashMap<>();
List<ScorePo>record= ScorePackage(urId, HarbourTermService().getId());
for(ScorePo n :record){
if(n !=null){
Map<String, Object> dataMap =new HashMap<>();
//模板的地址和新pdf的存储地址
//模板名称
dataMap.put("templatePath","成绩模板.pdf");
//pdf⽂件属性
dataMap.put("title","成绩单");
dataMap.put("author","****有限公司");
dataMap.put("subject","成绩单");
dataMap.put("keywords","report");
受伤的图片
dataMap.put("creator","****有限公司");
//表单属性,此处的属性,就是赋值给PDF模板的form表单的值
dataMap.put("name", n.getName()==null?"": n.getName());
dataMap.put("school", n.getSchoolName()==null?"": n.getSchoolName());
dataMap.put("courName", n.getChineName()==null?"": n.getChineName());
dataMap.put("teacher", n.getTeacherName()==null?"": n.getTeacherName())
map.Name(), dataMap);
}
}
return map;
}
2 将PDF通过压缩包形式下载
@ApiOperation(value ="下载成绩单")
@GetMapping(value ="export")
public void export(@RequestParam(name ="type") String type, HttpServletRequest request, HttpServletRespon respon)throws IOException {
String outputFileName ="score--"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip";
// 设置respon参数
<();
respon.addHeader("access-control-allow-origin","*");
respon.tHeader("Access-Control-Expo-Headers","Content-Disposition");
respon.tContentType("content-type:octet-stream;chart=UTF-8");
respon.tHeader("Content-Disposition","attachment;filename="+new String((outputFileName).getBytes(),"iso-8859-1"));
ServletOutputStream out = OutputStream();
ZipArchiveOutputStream zous =new ZipArchiveOutputStream(out);
zous.tUZip64(Zip64Mode.AsNeeded);
//此处的数据就是 1 中封装的map数据***********************
Map<String, Map<String, Object>> map = ScorePackage(Ur().getId());
for(Map.Entry<String, Map<String, Object>> entry : Set()){
String mapKey = Key();
Map<String, Object> value = Value();
烦的笔顺//此处是具体的将数据封装到 PDF 模板当中
ByteArrayOutputStream baoss = PDFBuilder.fillTemplate(value);
byte[] bytes = ByteArray();
//设置⽂件名
ArchiveEntry en =new ZipArchiveEntry(mapKey+".pdf");
zous.putArchiveEntry(en);
zous.write(bytes);水库游泳
zous.cloArchiveEntry();
if(baoss !=null){
baoss.clo();
}
}
if(zous!=null){
zous.clo();
}
}
3 将数据插⼊PDF具体操作
package;
import Document;
import DocumentException;
import Font;
import*;
import ClassPathResource;
显卡怎么看型号import ResourceUtils;
import*;
import HashMap;
import Iterator;
import Map;
public class PDFBuilder {
public static ByteArrayOutputStream fillTemplate(Map<String, Object> dataMap)throws IOException {// 利⽤模板⽣成pdf
// 模板路径
String templatePath = ("templatePath").toString();
ClassPathResource classPathResource =new ClassPathResource("template/"+templatePath);
InputStream inputStream = InputStream();
PdfReader reader;
ByteArrayOutputStream bos =new ByteArrayOutputStream();
五指毛桃炖鸡汤
PdfStamper stamper;
try{
try{
刘备父亲
reader =new PdfReader(inputStream);// 读取pdf模板
bos =new ByteArrayOutputStream();
stamper =new PdfStamper(reader, bos);
// 创建字体显⽰中⽂
BaFont bfChine = ateFont("STSongStd-Light","UniGB-UCS2-H", BaFont.NOT_EMBEDDED);
//查询出模板⽂件的表单域
Font font =new Font(bfChine,72,Font.BOLD);
AcroFields form = AcroFields();
Iterator<String> it = Fields().keySet().iterator();
while(it.hasNext()){
String name = it.next().toString();
if(!"signature_confirm".equals(name)){
form.tFieldProperty(name,"textfont",bfChine,null);
form.tField(name, (name).toString());
}el{
}
}
// 如果为fal那么⽣成的PDF⽂件还能编辑,⼀定要设为true
stamper.tFormFlattening(true);
stamper.clo();
// 1.创建⼀个ducument
Document document =new Document();
// 2.建⽴⼀个书写器(Writer)与document对象关联,通过书写器(Writer)可以将⽂档写⼊到磁盘中
// 创建 PdfWriter 对象第⼀个参数是对⽂档对象的引⽤,第⼆个参数是⽂件的实际名称,在该名称中还会给出其输出路径/*PdfWriter writer = Instance(document, out);
//PDF版本(默认1.4)
writer.tPdfVersion(PdfWriter.PDF_VERSION_1_6);*/
document.open();
// 设置属性
// 标题
document.addTitle("成绩单");
// 作者
document.addAuthor("***有限公司");
// 主题
document.addSubject("成绩单");
零之使魔漫画// 关键字
document.addKeywords("report");
// 创建时间
document.addCreationDate();
// 应⽤程序
document.addCreator("***有限公司");
// 5.关闭⽂档
document.clo();
bos.clo();
reader.clo();
对祖国的深情告白reader.clo();
}catch(IOException e){
e.printStackTrace();
}catch(DocumentException e){
e.printStackTrace();
}
return bos;
}
}
4 涉及到的包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>s</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
需要对PDF⽂件进⾏处理
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
具体的PDF模板我没有放上来,具体可以百度搜索PDF编辑器,form表单

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

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

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

标签:模板   数据   对象
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图