SpringBoot使用模板动态导出PDF使用itextpdf
SpringBoot使用模板动态导出PDF使用itextpdf
1.引包
----- dependencies部分-------------------------------------------------------------------------------
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!--字体集-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
----- build部分------------------------------------------------------------------------------- 配置所需字体包,将包放在resources路径下,我使用的是宋体,可以在本地C:\Windows\Fonts中拷贝需要使用的字体包
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>fonts
2.controller层代码段
@GetMapping(value = "/export", produces = "application/json")
@ResponseBody
public void export(HttpServletResponse response, SinCardTempFeeQueryVO sinCardTempFeeQueryVO) {
log.info("收据打印开始startPrint()");
response.setHeader("content-disposition","attachment;fileName="+"ReceiptPrinter.pdf");
try {
reportPrint.generateTempPDF(response,sinCardTempFeeQueryVO);
log.info("收据打印结束Print()");
} catch (Exception e) {
e.printStackTrace();
}
}
3.service层代码段
package com.lotso.web.module.KDFinfeeSure.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.lotso.web.common.utils.ConvertUpMoneyUtil;
import com.lotso.web.common.utils.HttpClientHelper;
import com.lotso.web.module.KDFinfeeSure.service.ReportPrintService;
import com.lotso.web.module.KDFinfeeSure.vo.JGSinCardQueryVO;
import com.lotso.web.module.KDFinfeeSure.vo.SinCardTempFeeQueryVO;
import com.lotso.web.module.system.entity.Dict;
import com.lotso.web.module.system.mapper.IDictMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class ReportPrintImpl implements ReportPrintService {
@Autowired
IDictMapper dictMapper;
@Autowired
private HttpClientHelper httpClient;
@Override
public void generateTempPDF(HttpServletResponse response, SinCardTempFeeQueryVO sinCardTempFeeQueryVO) throws Exception {
PdfReader reader = null;
PdfStamper ps = null;
OutputStream fos = null;
ByteArrayOutputStream bos = null;
InputStream fin = null;
ServletOutputStream out = null;
try {
String fileName = "/app/file/pdf/KDDLR_Receipt_Template.pdf";
reader = new PdfReader(fileName);
bos = new ByteArrayOutputStream();
ps = new PdfStamper(reader, bos);
BaseFont bf = BaseFont.createFont("/fonts/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
fontList.add(bf);
AcroFields fields = ps.getAcroFields();
fields.setSubstitutionFonts(fontList);
fillData(fields, data(sinCardTempFeeQueryVO));
ps.setFormFlattening(true);
if(ps != null){
ps.close();
}
fos = response.getOutputStream();
fos.write(bos.toByteArray());
}catch (Exception e){
e.printStackTrace();
log.error("异常:{",e.getMessage()+e.getCause()+"}");
}finally {
if(fos!=null){
fos.flush();
fos.close();
}
if (bos != null){
bos.close();
}
if(reader != null){
reader.close();
}
}
}
public void fillData(AcroFields fields, Map<String, String> data) {
try {
for (String key : data.keySet()) {
String value = data.get(key);
fields.setField(key, value);
}
} catch (Exception e) {
e.printStackTrace();
log.error("异常:{",e.getMessage()+e.getCause()+"}");
}
}
public Map<String, String> data( SinCardTempFeeQueryVO asinCardTempFeeQueryVO){
Map<String, String> paramters = new HashMap<String, String>();
paramters.put("prtseq",sinCardTempFeeQueryVO.getPrtseq());
paramters.put("agentcode",sinCardTempFeeQueryVO.getAgentcode());
paramters.put("agentname",sinCardTempFeeQueryVO.getAgentname());
paramters.put("oldprtseq",sinCardTempFeeQueryVO.getOldprtseq());
paramters.put("reqcom",sinCardTempFeeQueryVO.getReqcom());
return paramters;
}
}
4.前端处理
var confirmPay = function (obj) {
var link = document.createElement('a');
link.href = 'sincardtempfee/query/export?prtseq='+obj.prtseq+'&agentcode='+obj.agentcode+'&code='+obj.code;
link.click();
};
5.模板绘制
5.1.模板绘制工具
Adobe Acrobat DC
5.2.绘制模板
5.3.将模板引入项目,放在项目resource路径下
|