pdf根据模板导出

 
需要导入的包

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>



详细代码


public boolean getPdf(String ids,HttpServletRequest request, HttpServletResponse response,String isAll,YdTemplateOfSpfc ydTemplateOfSpfc) throws Exception {


List<YdTemplateOfSpfc> list= new ArrayList<YdTemplateOfSpfc>();
if (StringUtil.isNotBlank(isAll)){
Wrapper<YdTemplateOfSpfc> wrapper = new EntityWrapper<>();
if (StringUtil.isNotBlank(ydTemplateOfSpfc.getApplicantId())){
wrapper.where("applicant_id={0}",ydTemplateOfSpfc.getApplicantId());//查询该公司的记录
}
if (StringUtil.isNotBlank(ydTemplateOfSpfc.getTonYear())) {
wrapper.where("ton_year={0}", ydTemplateOfSpfc.getTonYear());
}
if(StringUtil.isNotBlank(ydTemplateOfSpfc.getUnloadingVesselName())){
wrapper.like("unloading_vessel_name", ydTemplateOfSpfc.getUnloadingVesselName());
}
if(StringUtil.isNotBlank(ydTemplateOfSpfc.getUnloadingVesselId())){
wrapper.like("receiving_vessel_name", ydTemplateOfSpfc.getReceivingVesselName());
}
wrapper.where("isdelete={0}","0");//过滤删除的
wrapper.where("report_state={0}","1");//过滤未上报的
List<YdTemplateOfSpfc> ydTemplateOfSpfcs = ydTemplateOfSpfcService.selectList( wrapper);
list= ydTemplateOfSpfcs;
if (StringUtil.isNotBlank(ydTemplateOfSpfc.getTonMonth())) {
String[] split = ydTemplateOfSpfc.getTonMonth().split(",");
if(split.length>1&&StringUtil.isNotBlank(split[0])){
list =new ArrayList<YdTemplateOfSpfc>();
for (int i = 0; i <ydTemplateOfSpfcs.size(); i++) {
if (Integer.parseInt(split[0])<= Integer.parseInt(ydTemplateOfSpfcs.get(i).getTonMonth())&&Integer.parseInt(split[1])>= Integer.parseInt(ydTemplateOfSpfcs.get(i).getTonMonth())){
list.add(ydTemplateOfSpfcs.get(i));
}
}
}
}
}else{
String[] split = ids.split(",");
System.out.println(ids);
for(int i = 0; i<split.length; i++) {
String id= split[i];
YdTemplateOfSpfc ydTemplateOfSpfcs = ydTemplateOfSpfcService.selectById(id);
list.add(ydTemplateOfSpfcs);
}
}
if (list.size()==0){
return false;
}


for(int i=0;i<list.size();i++) {
YdTemplateOfSpfc ydTemplateOfSpfcs = list.get(i);
// 指定解析器
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
//String path = request.getSession().getServletContext().getRealPath("/upload/");
// String path="e:/";
// response.setContentType("application/pdf");
// response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename, "UTF-8"));
OutputStream os = null;
PdfStamper ps = null;
PdfReader reader = null;
// 2 读入pdf表单
reader = new PdfReader("src/main/resources/NantaiReprintTable_model/ceshi.pdf");
os=new FileOutputStream(new File("src/main/resources/NantaiReprintTable_pdf/"+ydTemplateOfSpfcs.getAnticipatedDate()+ydTemplateOfSpfcs.getAndTime()+"NantaiReprintTable.pdf"));
// 3 根据表单生成一个新的pdf
ps = new PdfStamper(reader, os);
// 4 获取pdf表单
AcroFields form = ps.getAcroFields();

// 5给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
BaseFont bf = BaseFont.createFont("src/main/resources/NantaiReprintTable_zip/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H,true);
form.addSubstitutionFont(bf);


try {

// os = response.getOutputStream();


// 6查询数据================================================
Map<String, Object> data = new HashMap<String, Object>();
data.put("fill_1", ydTemplateOfSpfcs.getAnticipatedDate());
data.put("fill_2", ydTemplateOfSpfcs.getAndTime());
if (ydTemplateOfSpfcs.getAnticipatedLongitude().equals("W")){
ydTemplateOfSpfcs.setLongitudeNumber("-"+ ydTemplateOfSpfcs.getLongitudeNumber());
}
if (ydTemplateOfSpfcs.getAnticipatedLatitude().equals("S")){
ydTemplateOfSpfcs.setLatitudeNumber("-"+ ydTemplateOfSpfcs.getLatitudeNumber());
}
data.put("fill_3", ydTemplateOfSpfcs.getLatitudeNumber());
data.put("fill_4", ydTemplateOfSpfcs.getLongitudeNumber());
data.put("fill_5", ydTemplateOfSpfcs.getTranshippedFishSpecies());
data.put("fill_6", ydTemplateOfSpfcs.getVesselName());
data.put("fill_7", ydTemplateOfSpfcs.getUnloadingVesselName());
data.put("fill_8", ydTemplateOfSpfcs.getUnloadingRegistrationNumber());
data.put("fill_9", ydTemplateOfSpfcs.getUnloadingRadioCallSign());
data.put("fill_10", ydTemplateOfSpfcs.getUnloadingVesselFlag());
data.put("fill_11", ydTemplateOfSpfcs.getUnloadingImoNumber());
data.put("fill_12", ydTemplateOfSpfcs.getNameOfUnloadingVesselMaster());
data.put("fill_13", ydTemplateOfSpfcs.getNationalityOfUnloadingVesselMaster());
data.put("fill_14", ydTemplateOfSpfcs.getReceivingVesselName());
data.put("fill_15", ydTemplateOfSpfcs.getReceivingRegistrationNumber());
data.put("fill_16", ydTemplateOfSpfcs.getReceivingRadioCallSign());
data.put("fill_17", ydTemplateOfSpfcs.getReceivingVesselFlag());
data.put("fill_18", ydTemplateOfSpfcs.getUnloadingImoNumber());
data.put("fill_19", ydTemplateOfSpfcs.getNameOfReceivingVesselMaster());
data.put("fill_20", ydTemplateOfSpfcs.getNationalityOfReceivingVesselMaster());
// 7遍历data 给pdf表单表格赋值
for (String key : data.keySet()) {
form.setField(key,data.get(key).toString());
}
ps.setFormFlattening(true);
System.out.println("===============PDF导出成功=============");
} catch (Exception e) {
System.out.println("===============PDF导出失败=============");
e.printStackTrace();
} finally {
try {
ps.close();
reader.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

File f = new File("src/main/resources/NantaiReprintTable_pdf");

new CompressUtil(new File("src/main/resources/NantaiReprintTable_zip/", f.getName() + ".zip")).zipFiles(f);

InputStream in=new FileInputStream("src/main/resources/NantaiReprintTable_zip/NantaiReprintTable_pdf.zip");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//创建存放文件内容的数组
byte[] buff =new byte[1024];
//所读取的内容使用n来接收
int n;
//当没有读取完时,继续读取,循环
while((n=in.read(buff))!=-1){
//将字节数组的数据全部写入到输出流中
outputStream.write(buff,0,n);
}
//强制将缓存区的数据进行输出
outputStream.flush();
//关流
outputStream.close();
in.close();


deleteDir("src/main/resources/NantaiReprintTable_pdf");
deleteDir("src/main/resources/NantaiReprintTable_zip/NantaiReprintTable_pdf.zip");
return true;
}

转载于:https://www.cnblogs.com/zhlkeep/p/10854777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值