一、需求背景
近日接触到一个需求,根据PDF模板动态的生成PDF文件并下载,模板中包括包括各种文本数据以及统计图,统计图是需要动态替换的。需求难点在于PDF模板的制作与统计图的生成。
二、涉及工具
1、 Adobe Acrobat DC(PDF文件编辑工具,制作PDF模板需要用到),贴心的我已经准备好了下载地址:pan.baidu.com/s/1io2eZ7sN… 提取码: ws6y
2、 itextpdf:此组件用于操作PDF文档
3、 jfreechart:此组件用于在后端制作统计图
话不多说,直接实战!!
三、实战
1、制作模板
首先新建一个word文档,在文档中编写模板样式,如图:
文件另存为成 pdf 格式。
打开Adobe Acrobat DC软件,编辑PDF模板。
文件-----> 创建------> 创建表单-------> 选择之前保存的PDF文件-------> 点击开始
设置的文本域名称就是到时候需要在程序中替换的key的值。 制作完模板后保存退出。
至此,模板制作完毕。
2、后端项目搭建
pom文件中引入相关包:
<!-- pdf操作类 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--用于jfreechart制作统计图 -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.5.0</version>
</dependency>
将制作好的模板放入 resource 下
下载PDF接口:
@RequestMapping(value = "/exportPdf")
public String exportPdf(HttpServletResponse response){
// 读取pdf模板
ClassPathResource classPathResource = new ClassPathResource("templates/报告模板pdf_final.pdf");
// 输出路径
String targetPath = JfreechartUtil.getAbsoluteFile("202203.pdf");
PdfReader reader;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
FileOutputStream out;
try {
out = new FileOutputStream(targetPath);// 输出流
reader = new PdfReader(classPathResource.getInputStream());
int numberOfPages = reader.getNumberOfPages();
bos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
// 准备替换的数据
Map<String, String> map = getValueMapInfo();
// 文本内容处理
fillPdfCellForm(map, form);
Map<String, String> imageMap = getImageMapInfo();
// 图片内容处理
fillPdfImage(imageMap,form,stamper);
// true代表生成的PDF文件不可编辑
stamper.setFormFlattening(true);
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
// 将所有页面添加到pdf中
for (int i = 1; i <= numberOfPages; i++) {
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
copy.addPage(importPage);
}
doc.close();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment;filename=" + new String("202203.pdf".getBytes(), "iso8859-1"));
ServletOutputStream os = response.getOutputStream();
File file = new File(targetPath);
InputStream input = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len=0;
while ((len = input.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
os.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
return "下载成功";
}
/**
* 准备替换内容数据
* @return
*/
public Map<String, String> getValueMapInfo(){
Map<String, String> map = new HashMap<>();
map.put("name1","100200");
map.put("name2","703020");
map.put("name3","23.8%");
map.put("name4","9.7%");
map.put("name5","1.3%");
map.put("name6","16");
map.put("name7","2522");
map.put("name8","7234");
map.put("name9","31.9%");
map.put("name10","6.8%");
map.put("name11","29.2");
map.put("name12","2.7%");
map.put("name13","5.22%");
map.put("name14","1.76%");
return map;
}
// 文本处理 替换模板内容
private static void fillPdfCellForm(Map<String, String> map, AcroFields form) throws IOException, DocumentException {
for (Map.Entry entry : map.entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
form.setField(key, value);
}
}
// 制作统计图并将统计图生成为图片
public Map<String, String> getImageMapInfo(){
Map<String, Double> map=new HashMap<String, Double>();
map.put("冠心病", (double) 1000);
map.put("脑卒中", (double) 700);
map.put("肺结核", (double) 600);
map.put("糖尿病", (double) 400);
map.put("高血压", (double) 100);
map.put("精神病", (double) 2000);
String image1Url = JfreechartUtil.getAbsoluteFile("image1.jpg");
JfreechartUtil.createPiePort("慢病统计结果", map,image1Url);
Map<String, Double> map1=new HashMap<String, Double>();
//设置第一期的投票信息
map1.put("2020-02-03", (double) 700);
map1.put("2020-02-04", (double) 1000);
map1.put("2020-02-05", (double) 600);
map1.put("2020-02-06", (double) 400);
map1.put("2020-02-07", (double) 4000);
map1.put("2020-02-08", (double) 1200);
map1.put("2020-02-09", (double) 800);
String image2Url = JfreechartUtil.getAbsoluteFile("image2.jpg");
JfreechartUtil.createLinePort("近7日金额(日报)",map1,"日期","金额(元)",image2Url);
Map<String, String> imageMap = new HashMap<>();
imageMap.put("image1",image1Url);
imageMap.put("image2",image2Url);
return imageMap;
}
// 图片处理
private static void fillPdfImage(Map<String, String> map, AcroFields form, PdfStamper stamper) throws IOException, DocumentException {
for(String key : map.keySet()) {
String value = map.get(key);
String imgpath = value;
int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
//根据路径读取图片
Image image = Image.getInstance(imgpath);
//获取图片页面
PdfContentByte under = stamper.getOverContent(pageNo);
//图片大小自适应
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
//添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
}
}
jfreechart工具类:
package com.example.springbootceshi.util;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class JfreechartUtil {
private static String NO_DATA_MSG = "暂无数据";
private static Font FONT = new Font("宋体", Font.PLAIN, 20);
public static Color[] CHART_COLORS = {
new Color(31,129,188), new Color(92,92,97), new Color(144,237,125), new Color(255,188,117),
new Color(153,158,255), new Color(255,117,153), new Color(253,236,109), new Color(128,133,232),
new Color(158,90,102),new Color(255, 204, 102) };// 颜色
static{
setChartTheme();
}
/**
* 中文主题样式 解决乱码
*/
public static void setChartTheme() {
// 设置中文主题样式 解决乱码
StandardChartTheme chartTheme = new StandardChartTheme("CN");
// 设置标题字体
chartTheme.setExtraLargeFont(FONT);
// 设置图例的字体
chartTheme.setRegularFont(FONT);
// 设置轴向的字体
chartTheme.setLargeFont(FONT);
chartTheme.setSmallFont(FONT);
chartTheme.setTitlePaint(new Color(51, 51, 51));
chartTheme.setSubtitlePaint(new Color(85, 85, 85));
chartTheme.setLegendBackgroundPaint(Color.WHITE);// 设置标注
chartTheme.setLegendItemPaint(Color.BLACK);//
chartTheme.setChartBackgroundPaint(Color.WHITE);
// 绘制颜色绘制颜色.轮廓供应商
// paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequence
Paint[] OUTLINE_PAINT_SEQUENCE = new Paint[] { Color.WHITE };
// 绘制器颜色源
DefaultDrawingSupplier drawingSupplier = new DefaultDrawingSupplier(CHART_COLORS, CHART_COLORS, OUTLINE_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);
chartTheme.setDrawingSupplier(drawingSupplier);
chartTheme.setPlotBackgroundPaint(Color.WHITE);// 绘制区域
chartTheme.setPlotOutlinePaint(Color.WHITE);// 绘制区域外边框
chartTheme.setLabelLinkPaint(new Color(8, 55, 114));// 链接标签颜色
chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
chartTheme.setAxisOffset(new RectangleInsets(5, 12, 5, 12));
chartTheme.setDomainGridlinePaint(new Color(192, 208, 224));// X坐标轴垂直网格颜色
chartTheme.setRangeGridlinePaint(new Color(192, 192, 192));// Y坐标轴水平网格颜色
chartTheme.setBaselinePaint(Color.WHITE);
chartTheme.setCrosshairPaint(Color.BLUE);// 不确定含义
chartTheme.setAxisLabelPaint(new Color(51, 51, 51));// 坐标轴标题文字颜色
chartTheme.setTickLabelPaint(new Color(67, 67, 72));// 刻度数字
chartTheme.setBarPainter(new StandardBarPainter());// 设置柱状图渲染
chartTheme.setXYBarPainter(new StandardXYBarPainter());// XYBar 渲染
chartTheme.setItemLabelPaint(Color.black);
chartTheme.setThermometerPaint(Color.white);// 温度计
ChartFactory.setChartTheme(chartTheme);
}
/**
* 必须设置文本抗锯齿
*/
public static void setAntiAlias(JFreeChart chart) {
chart.setTextAntiAlias(false);
}
/**
* 设置图例无边框,默认黑色边框
*/
public static void setLegendEmptyBorder(JFreeChart chart) {
chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
}
/**
* 提供静态方法:获取报表图形1:饼状图
* @param title 标题
* @param datas 数据
* @param url 字体
*/
public static void createPiePort(String title, Map<String,Double> datas, String url){
try {
// 如果不使用Font,中文将显示不出来
DefaultPieDataset pds = new DefaultPieDataset();
// 获取迭代器:
Set<Map.Entry<String, Double>> set = datas.entrySet();
Iterator iterator=(Iterator) set.iterator();
Map.Entry entry=null;
while(iterator.hasNext()){
entry=(Map.Entry) iterator.next();
pds.setValue(entry.getKey().toString(),Double.parseDouble(entry.getValue().toString()));
}
/**
* 生成一个饼图的图表
* 分别是:显示图表的标题、需要提供对应图表的DateSet对象、是否显示图例、是否生成贴士以及是否生成URL链接
*/
JFreeChart chart = ChartFactory.createPieChart(title, pds, true, true, true);
setPieRender((PiePlot) chart.getPlot());
//将内存中的图片写到本地硬盘
org.jfree.chart.ChartUtils.saveChartAsPNG(new File(url), chart,800,500);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置饼状图渲染
*/
public static void setPieRender(Plot plot) {
plot.setNoDataMessage(NO_DATA_MSG);
plot.setInsets(new RectangleInsets(10, 10, 5, 10));
PiePlot piePlot = (PiePlot) plot;
piePlot.setInsets(new RectangleInsets(0, 0, 0, 0));
piePlot.setCircular(true);// 圆形
// piePlot.setSimpleLabels(true);// 简单标签
piePlot.setLabelGap(0.01);
piePlot.setInteriorGap(0.05D);
piePlot.setLegendItemShape(new Rectangle(10, 10));// 图例形状
piePlot.setIgnoreNullValues(true);
piePlot.setLabelBackgroundPaint(null);// 去掉背景色
piePlot.setLabelShadowPaint(null);// 去掉阴影
piePlot.setLabelOutlinePaint(null);// 去掉边框
piePlot.setShadowPaint(null);
// 0:category 1:value:2 :percentage
piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{2}"));// 显示标签数据
}
/**
* 提供静态方法:获取报表图形1:柱状图
* @param title 标题
* @param xName X轴名称
* @param yName Y轴名称
* @param datas 数据
* @param url 字体
*/
public static void createBarPort(String title, String xName, String yName, Map<String,Double> datas, String url){
try {
// 如果不使用Font,中文将显示不出来
DefaultCategoryDataset pds = new DefaultCategoryDataset();
// 获取迭代器:
Set<Map.Entry<String, Double>> set = datas.entrySet();
Iterator iterator=(Iterator) set.iterator();
Map.Entry entry=null;
while(iterator.hasNext()){
entry=(Map.Entry) iterator.next();
pds.setValue(Double.parseDouble(entry.getValue().toString()),entry.getKey().toString(),entry.getKey().toString());
}
/**
* 生成一个柱状的图表
* 分别是:显示图表的标题、需要提供对应图表的DateSet对象、是否显示图例、是否生成贴士以及是否生成URL链接
*/
JFreeChart chart = ChartFactory.createBarChart(title,xName,yName, pds, PlotOrientation.VERTICAL, true, true, false);
//将内存中的图片写到本地硬盘
org.jfree.chart.ChartUtils.saveChartAsPNG(new File(url), chart,800,500);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 提供静态方法:获取报表图形3:折线图
* @param title 标题
* @param datas 数据
* @param xName X轴名称
* @param yName Y轴名称
* @param url 保存地址
*/
public static void createLinePort(String title,Map<String,Double> datas,String xName,String yName,String url){
try {
//种类数据集
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//获取迭代器:
Set<Map.Entry<String, Double>> set = datas.entrySet();
Iterator iterator=(Iterator) set.iterator();
Map.Entry entry=null;
while(iterator.hasNext()){
entry=(Map.Entry) iterator.next();
dataset.setValue(Double.parseDouble(entry.getValue().toString()),//y
title, //名称
entry.getKey().toString()); //x
}
//创建折线图,折线图分水平显示和垂直显示两种
JFreeChart chart = ChartFactory.createLineChart(title, xName, yName, dataset,//2D折线图
PlotOrientation.VERTICAL,
false, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
true);// 是否生成URL链接
//得到绘图区
setLineRender((CategoryPlot)chart.getPlot(),true,true);
org.jfree.chart.ChartUtils.saveChartAsPNG(new File(url), chart, 1000,600);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置折线图样式
*
* @param plot
* @param isShowDataLabels
* 是否显示数据标签
*/
public static void setLineRender(CategoryPlot plot, boolean isShowDataLabels, boolean isShapesVisible) {
plot.setNoDataMessage(NO_DATA_MSG);
plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);
// 获取折线对象
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setDefaultStroke(new BasicStroke(1.5F));
// 设置折线颜色
renderer.setSeriesFillPaint(1, new Color(254, 103, 0));
renderer.setSeriesPaint(1, new Color(254, 103, 0));
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShapesVisible(1, true);
if (isShowDataLabels) {
renderer.setDefaultItemLabelsVisible(true);
renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator(StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING,
NumberFormat.getInstance()));
renderer.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));// weizhi
}
renderer.setDefaultShapesVisible(isShapesVisible);// 数据点绘制形状
setXAixs(plot);
setYAixs(plot);
}
/**
* 设置类别图表(CategoryPlot) X坐标轴线条颜色和样式
*
* @param plot
*/
public static void setXAixs(CategoryPlot plot) {
Color lineColor = new Color(31, 121, 170);
// 设置横轴文本倾斜度
plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
plot.getDomainAxis().setAxisLinePaint(lineColor);// X坐标轴颜色
plot.getDomainAxis().setTickMarkPaint(lineColor);// X坐标轴标记|竖线颜色
}
/**
* 设置类别图表(CategoryPlot) Y坐标轴线条颜色和样式 同时防止数据无法显示
*
* @param plot
*/
public static void setYAixs(CategoryPlot plot) {
// Color lineColor = new Color(192, 208, 224);
Color lineColor = new Color(31, 121, 170);
ValueAxis axis = plot.getRangeAxis();
axis.setAxisLinePaint(lineColor);// Y坐标轴颜色
axis.setTickMarkPaint(lineColor);// Y坐标轴标记|竖线颜色
// false隐藏Y刻度
axis.setAxisLineVisible(true);
axis.setTickMarksVisible(true);
// Y轴网格线条
plot.setRangeGridlinePaint(new Color(192, 192, 192));
plot.setRangeGridlineStroke(new BasicStroke(1));
plot.getRangeAxis().setUpperMargin(0.1);// 设置顶部Y坐标轴间距,防止数据无法显示
plot.getRangeAxis().setLowerMargin(0.1);// 设置底部Y坐标轴间距
}
/**
* 设置XY图表(XYPlot) X坐标轴线条颜色和样式
*
* @param plot
*/
public static void setXY_XAixs(XYPlot plot) {
Color lineColor = new Color(31, 121, 170);
plot.getDomainAxis().setAxisLinePaint(lineColor);// X坐标轴颜色
plot.getDomainAxis().setTickMarkPaint(lineColor);// X坐标轴标记|竖线颜色
}
/**
* 设置XY图表(XYPlot) Y坐标轴线条颜色和样式 同时防止数据无法显示
*
* @param plot
*/
public static void setXY_YAixs(XYPlot plot) {
Color lineColor = new Color(192, 208, 224);
ValueAxis axis = plot.getRangeAxis();
axis.setAxisLinePaint(lineColor);// X坐标轴颜色
axis.setTickMarkPaint(lineColor);// X坐标轴标记|竖线颜色
// 隐藏Y刻度
axis.setAxisLineVisible(false);
axis.setTickMarksVisible(false);
// Y轴网格线条
plot.setRangeGridlinePaint(new Color(192, 192, 192));
plot.setRangeGridlineStroke(new BasicStroke(1));
plot.setDomainGridlinesVisible(false);
plot.getRangeAxis().setUpperMargin(0.12);// 设置顶部Y坐标轴间距,防止数据无法显示
plot.getRangeAxis().setLowerMargin(0.12);// 设置底部Y坐标轴间距
}
/**
* 创建多条折线图
* @param dataset 数据集
* @param title 标题
* @param xName X轴名称
* @param yName Y轴名称
* @param url 保存地址
*/
public static void createLinesChart(DefaultCategoryDataset dataset, String title, String xName, String yName, String url){
JFreeChart chart = ChartFactory.createLineChart(
title, // 统计图标题
xName, // 横轴显示信息
yName, // 纵轴显示信息
dataset, // 显示数据
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
//得到绘图区
setLineRender((CategoryPlot)chart.getPlot(),true,true);
try {
org.jfree.chart.ChartUtils.saveChartAsPNG(new File(url), chart, 1000,600);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取下载路径
*
* @param filename 文件名称
*/
public static String getAbsoluteFile(String filename)
{
String downloadPath = "D:/jfreechart/" + filename;
File desc = new File(downloadPath);
if (!desc.getParentFile().exists())
{
desc.getParentFile().mkdirs();
}
return downloadPath;
}
}
绘制多条折线图时,数据集准备:
// 数据集
public static void createDataSet(){
// 折线代表数据 一条代表2021年数据,一条代表2022年数据
String series1 = "2021";
String series2 = "2022";
// 横轴数据
String type1 = "1月";
String type2 = "2月";
String type3 = "3月";
String type4 = "4月";
String type5 = "5月";
String type6 = "6月";
String type7 = "7月";
String type8 = "8月";
// 纵轴数据
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, type1);
dataset.addValue(4.0, series1, type2);
dataset.addValue(3.0, series1, type3);
dataset.addValue(5.0, series1, type4);
dataset.addValue(5.0, series1, type5);
dataset.addValue(7.0, series1, type6);
dataset.addValue(7.0, series1, type7);
dataset.addValue(8.0, series1, type8);
dataset.addValue(5.0, series2, type1);
dataset.addValue(7.0, series2, type2);
dataset.addValue(6.0, series2, type3);
dataset.addValue(8.0, series2, type4);
dataset.addValue(4.0, series2, type5);
dataset.addValue(4.0, series2, type6);
dataset.addValue(2.0, series2, type7);
dataset.addValue(1.0, series2, type8);
String image3Url = JfreechartUtil.getAbsoluteFile("image3.jpg");
JfreechartUtil.createLinesChart(dataSet,"销量月趋势","月份","数量",image3Url);
}
测试,启动项目访问下载pdf接口http://localhost:8090/ceshi/pdf/exportPdf,下载后打开效果如下: