springboot集成itext根据模板导出PDF
项目需要根据一个已知的模板文档,取数据然后填充空格。
先制作word版模板,然后另存为pdf格式,转换后使用Adobe Acrobat Pro添加域,生成PDF模板。
利用itext读取模板,填充数据,下载导出。
参考大神链接:https://blog.csdn.net/tyc_054600/article/details/104060362
具体步骤:
1、pom引用
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.11</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
2、创建PdfUtil类
package com.ruoyi.common.utils.itext;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.ruoyi.common.config.RuoYiConfig;
import java.net.URLEncoder;
import org.apache.commons.io.IOUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;
import java.util.List;
/**
* @Description
* @auther Tian
* @Date 2020/4/11 13:28
**/
public class PdfUtil {
/**
* 利用模板生成pdf保存到某路径下
*/
public static String pdfOut(String modalFilename,String newFilename,Map<String, Object> inputMap) {
// 生成的新文件路径
String path0 = RuoYiConfig.getReportPath();
File f = new File(path0);
if (!f.exists()) {
f.mkdirs();
}
// 模板路径
String templatePath = RuoYiConfig.getReportPath() + modalFilename;
// 创建文件夹
String newPdfPath = RuoYiConfig.getDownloadPath() + newFilename;
File file = new File(templatePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
File file1 = new File(newPdfPath);
if (!file1.exists()) {
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
String path = "C:/WINDOWS/Fonts/simsun.ttc,0";//windows里的字体资源路径
BaseFont bf = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 输出流
out = new FileOutputStream(newPdfPath);
// 读取pdf模板
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//文字类的内容处理
Map<String, String> datemap = (Map<String, String>) inputMap.get("dateMap");
form.addSubstitutionFont(bf);
for (String key : datemap.keySet()) {
String value = datemap.get(key);
form.setField(key, value);
}
// 表格类
Map<String, List<List<String>>> listMap = (Map<String, List<List<String>>>) inputMap.get("list");
for (String key : listMap.keySet()) {
List<List<String>> lists = listMap.get(key);
int pageNo = form.getFieldPositions(key).get(0).page;
PdfContentByte pcb = stamper.getOverContent(pageNo);
Rectangle signRect = form.getFieldPositions(key).get(0).position;
//表格位置
int column = lists.get(0).size();
PdfPTable table = new PdfPTable(column);
float tatalWidth = signRect.getRight() - signRect.getLeft() - 1;
int size = lists.get(0).size();
float width[] = new float[size];
for (int i = 0; i < size; i++) {
if (i == 0) {
width[i] = 60f;
} else {
width[i] = (tatalWidth - 60) / (size - 1);
}
}
table.setTotalWidth(width);
table.setLockedWidth(true);
table.setKeepTogether(true);
table.setSplitLate(false);
table.setSplitRows(true);
Font FontProve = new Font(bf, 12, 0);
//表格数据填写
int rowFirstPage=21;//第一页最大行数
int row = lists.size()>=rowFirstPage?rowFirstPage:lists.size();
for (int i = 0; i < lists.size(); i++) {
List<String> list = lists.get(i);
for (int j = 0; j < column; j++) {
Paragraph paragraph = new Paragraph(String.valueOf(list.get(j)), FontProve);
PdfPCell cell = new PdfPCell(paragraph);
cell.setBorderWidth(0.5f);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(0, (float) 1.4);
table.addCell(cell);
}
}
table.writeSelectedRows(0, row, signRect.getLeft(), signRect.getTop(), pcb);//从指定位置写入表格
//表格内容超出一页
if(lists.size()>rowFirstPage){
int rowOtherPage=25;//新增页表格行数
int pageAddNum=(lists.size()-rowFirstPage)/rowOtherPage;//待增加页数
if((lists.size()-rowFirstPage)%rowOtherPage>0){
pageAddNum++;
}
for(int i=0;i<pageAddNum;i++){
int rowNowStart=i*rowOtherPage+rowFirstPage;
int rowNowEnd=rowNowStart+rowOtherPage;
if(rowNowEnd>lists.size()){
rowNowEnd=lists.size();
}
stamper.insertPage(reader.getNumberOfPages() + 1, reader.getPageSizeWithRotation(1));//新增空白页
PdfContentByte under = stamper.getOverContent(reader.getNumberOfPages());//捕获新增的空白页
table.writeSelectedRows(rowNowStart, rowNowEnd, signRect.getLeft(), 750, under);
}
}
}
stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
stamper.close();
Document doc = new Document(PageSize.LETTER.rotate());//pdf横向打开
doc.setPageSize(PageSize.A4);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
int pages = stamper.getReader().getNumberOfPages();
for (int i = 1; i <= pages; i++) {
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
copy.addPage(importPage);
}
doc.close();
out.close();
reader.close();
return newFilename;
} catch (IOException | DocumentException e) {
System.out.println(e);
}
return "";
}
/**
* 利用模板生成pdf导出
*/
public static void pdfExport(String modalFilename,String newFilename, HttpServletResponse response,Map<String, Object> inputMap) {
// 生成的新文件路径
String path0 = RuoYiConfig.getReportPath();
File f = new File(path0);
if (!f.exists()) {
f.mkdirs();
}
// 模板路径
String templatePath = RuoYiConfig.getReportPath()+modalFilename;
File file = new File(templatePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
PdfReader reader;
ByteArrayOutputStream bos;
PdfStamper stamper;
OutputStream out = null;
try {
Map<String, String> datemap = (Map<String, String>) inputMap.get("dateMap");
String path = "C:/WINDOWS/Fonts/simsun.ttc,0";//windows里的字体资源路径
BaseFont bf = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 输出流
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(newFilename, "UTF-8"));
out = new BufferedOutputStream(response.getOutputStream());
// 读取pdf模板
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//文字类的内容处理
form.addSubstitutionFont(bf);
for (String key : datemap.keySet()) {
String value = datemap.get(key);
form.setField(key, value);
}
stamper.setFormFlattening(false);
stamper.close();
Document doc = new Document(PageSize.LETTER.rotate());//pdf横向打开
doc.setPageSize(PageSize.A4);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
out.close();
reader.close();
} catch (IOException | DocumentException e) {
System.out.println(e);
} finally {
try {
assert out != null;
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取本地pdf,这里设置的是预览
*/
public static void readPdf(String filename,HttpServletResponse response) {
response.reset();
response.setContentType("application/pdf");
try {
File file = new File(RuoYiConfig.getReportPath()+filename);
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
response.setHeader("Content-Disposition", "inline; filename= "+filename);
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、创建Controller
//导出PDF报表
@GetMapping("/downReport/{id}")
public AjaxResult downloadPDF(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) throws Exception {
String modalFilename="taskreport.pdf";
String newFilename="记录单.pdf";
Map<String, Object> dataMap=taskService.getModalData(id);//获取导出模板中需要填充的数据
//生成文件放入download文件夹,返回生成文件名称
return AjaxResult.success(PdfUtil.pdfOut(modalFilename, newFilename,dataMap));
}
4、获取导出模板中需要填充的数据
//查找导出数据
@Override
public Map<String, Object> getModalData(Long id) {
Map<String, Object> dataMap=new HashMap<>();
Map<String, String> dataMap1 = new HashMap<>();
Task task = taskMapper.selectTaskById(id);
dataMap1.put("id", id.toString());
dataMap1.put("taskType", changeTaskType(task.getTaskType()));
dataMap1.put("createBy", task.getCreateBy());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(task.getCreateTime());
dataMap1.put("createTime", dateString);
dataMap.put("dateMap",dataMap1);//获取填充PDF的数据
//设备列表
TaskItem taskItem =new TaskItem();
taskItem.setTaskId(id);
List<TaskItem> taskItems=taskMapper.selectTaskItemList(taskItem);
List<List<String>> List = new ArrayList<List<String>>();
List<String> listHead = new ArrayList<String>();
listHead.add("序号");
listHead.add("编号");
listHead.add("类型");
List.add(listHead);
int i=1;
for (TaskItem item:taskItems) {//一行一个list
List<String> list = new ArrayList<String>();
list.add(i+"");
list.add(item.getSenu());
list.add(item.getTypeModel());
List.add(list);
i++;
}
Map<String, List<List<String>>> listMap = new HashMap<String, List<List<String>>>();
listMap.put("deviceLists", List);
dataMap.put("list", listMap);
return dataMap;
}
5、页面添加下载按钮,如果没有form可JS添加
function downPdf(){
var url = '/system/reportexport/downPdf/';
var form = $("<form>");//定义一个form表单
form.attr("style", "display:none");
form.attr("target", "");
form.attr("method", "get"); //请求类型
form.attr("action", url); //请求地址
$("body").append(form);//将表单放置在web中
form.submit();//表单提交
}
6、制作模板
在指定位置添加pdf模板,先用word编辑好要到处的模板,另存为可以存为PDF格式。
转换为PDF后使用Adobe Acrobat Pro(网上有破解版,下载时注意不要附加其他的软件下载),打开要使用的PDF,
表单->添加或编辑域->修改域名(域名即为待填充的字段名称)
此时会默认添加很多域,可以手动增加或删除域,双击域可修改域的名称、可读属性、字体大小和样式。
7、字体问题
中文问题:
String path = "C:/WINDOWS/Fonts/simsun.ttc,0";//windows里的字体资源路径
BaseFont bf = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
有些中文显示不全 企业名称显示企名称,但是预览点击时显示企业名称,预览和打印显示企名称
考虑是字体包的问题,替换字体包 为仿宋 String path = "C:/WINDOWS/Fonts/simsun.ttc,0";//windows里的字体资源路径,正常显示
本地查看电脑所带字体:C:/WINDOWS/Fonts,这个文件夹下的字体,右键打开属性,会看到文件名。个人建议使用宋体,使用windows自带字体,这样兼容性会高很多。
参考大神链接:
1、https://www.cnblogs.com/whalesea/p/11752086.html
2、https://www.cnblogs.com/whalesea/p/11714681.html
3、https://blog.csdn.net/yangdonghhm/article/details/84239196
8、下载后使用AdobeReader打开会报错,缺少字体包
可以使用右击,选择浏览器打开
或者是AdobeReader字体包的原因
使用Adobe Acrobat Pro域编辑时,选择域属性,选择字体为生成文件选用的字体,windows系统自带字体包含宋体,使用宋体可以
9、下载后不可编辑
域属性选择只读,否则下载的文件就可以编辑,在AdobeReader中打开这些位置还会显示蓝色。
10、增加权限
https://www.cnblogs.com/matrix-zhu/p/6305944.html
浙公网安备 33010602011771号