前言

有个人(死需求)跑过来跟你说,这些都给我输出成报告,pdf格式的,所以就有了下面这个,做一下笔记,以后有用直接过来拿。在网上找了一下,发现大家都是在用itext。
iText是著名的开放项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

http://itextpdf.com/

maven依赖

        <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>

基础操作

itext有很多功能,这里先说基本的操作。其他更多高级的操作,可以继续看下面的。
基本处理步骤如下伪代码:

//Step 1—Create a Document.
Document document = new Document();
//Step 2—Get a PdfWriter instance.
PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createSamplePDF.pdf"));
//Step 3—Open the Document.
document.open();
//Step 4—Add content.
document.add(new Paragraph("Hello World"));
//Step 5—Close the Document.
document.close();

1、直接输出数据到pdf文件

这里有个特别注意的是,中文必须要指定字体,即是BaseFont

public class PDFReport {private final static String REPORT_PATH = "C:/air-navi-monitor/report";private static void exportReport() {BaseFont bf;Font font = null;Font font2 = null;try {bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//创建字体font = new Font(bf, 12);//使用字体font2 = new Font(bf, 12, Font.BOLD);//使用字体} catch (Exception e) {e.printStackTrace();}Document document = new Document();try {PdfWriter.getInstance(document, new FileOutputStream("E:/2.pdf"));document.open();Paragraph elements = new Paragraph("常州武进1区飞行报告", font2);elements.setAlignment(Paragraph.ALIGN_CENTER);document.add(elements);Image png = Image.getInstance("E:\\test.png");png.setAlignment(Image.ALIGN_CENTER);document.add(png);document.add(new Paragraph("任务编号:20190701        开始日期:20190701", font));document.add(new Paragraph("任务名称:常州武进1区     结束日期:20190701", font));document.add(new Paragraph("平均飞行高度:100m        平均飞行速度:100km/h", font));document.add(new Paragraph("任务面积:1000㎡      结束日期:20190701", font));document.add(new Paragraph("飞行总时长:1000㎡", font));document.addCreationDate();document.close();} catch (Exception e) {System.out.println("file create exception");}}/*** 生成pdf文件** @param missionReport* @return*/public static String exportReport(MissionReportTb missionReport) throws AirNaviException {String pdfPath = null;String imgPath = Shape2Image.getImgPath(missionReport.getMissionID());
//        String imgPath = "E:\\test.png";String finalReportStr = missionReport.getMissionReport();MissionReport finalReport = JSONObject.parseObject(finalReportStr, MissionReport.class);BaseFont bf;Font font = null;Font font2 = null;try {bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//创建字体font = new Font(bf, 12);//使用字体font2 = new Font(bf, 12, Font.BOLD);//使用字体} catch (Exception e) {e.printStackTrace();}Document document = new Document();try {File dir = new File(REPORT_PATH);if (!dir.exists()) {dir.mkdirs();}File file = new File(REPORT_PATH + File.separator + missionReport.getMissionID() + ".pdf");if (!file.exists()) {file.createNewFile();}PdfWriter.getInstance(document, new FileOutputStream(REPORT_PATH + File.separator + missionReport.getMissionID() + ".pdf"));document.open();Paragraph elements = new Paragraph(missionReport.getMissionName() + "飞行报告", font2);elements.setAlignment(Paragraph.ALIGN_CENTER);document.add(elements);Image png = Image.getInstance(imgPath);
//            https://blog.csdn.net/lingbo89/article/details/76177825float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();float documentHeight = documentWidth / 580 * 320;//重新设置宽高png.scaleAbsolute(documentWidth, documentHeight);//重新设置宽高png.scalePercent(50);// 根据域的大小缩放图片
//            image.scaleToFit(signRect.getWidth(), signRect.getHeight());png.setAlignment(Image.ALIGN_CENTER);document.add(png);document.add(new Paragraph("任务编号:" + missionReport.getMissionCode() + ",开始日期:" + finalReport.getStartTime(), font));document.add(new Paragraph("任务名称:" + missionReport.getMissionName() + ",结束日期:" + finalReport.getEndTime(), font));document.add(new Paragraph("平均飞行高度:" + finalReport.getAvgFlightHeight() + "m" + ",平均飞行速度:" + finalReport.getAvgFlightSpeed() + "km/h", font));document.add(new Paragraph("任务面积:" + finalReport.getMissionArea() + "㎡" + ",飞行总时长:" + finalReport.getFlightDuration() + "min", font));document.addCreationDate();document.close();pdfPath = file.getAbsolutePath();} catch (Exception e) {e.printStackTrace();log.error(e.getMessage());System.out.println("file create exception");throw new AirNaviException("生成PDF失败:" + e.getMessage());}return pdfPath;}public static void main(String[] args) throws AirNaviException {String report = "{\"detailMissionReport\":[{\"avgFlightHeight\":119.7,\"avgFlightSpeed\":71.1,\"endPoint\":\"113.27484,22.86843\",\"endTime\":\"2019-09-17 17:47:07\",\"flightDuration\":9,\"reportID\":1,\"startPoint\":\"113.31429,22.78240\",\"startTime\":\"2019-09-17 17:38:03\",\"statisticsTimes\":505}],\"missionReport\":{\"avgFlightHeight\":119.7,\"avgFlightSpeed\":71.1,\"endPoint\":\"113.31429,22.78240\",\"endTime\":\"2019-09-17 17:47:07\",\"flightDuration\":9,\"reportID\":1,\"startPoint\":\"113.31429,22.78240\",\"startTime\":\"2019-09-17 17:38:03\",\"statisticsTimes\":0},\"missionArea\":0.0,\"missionCode\":\"M001\",\"missionID\":\"888813ddef6646cd9bfaba5abb748a43\",\"missionName\":\"德胜航点M008\",\"missionStatus\":1,\"missionType\":0,\"plannedFlightTime\":\"20190909\"}";MissionReportTb missionReportTb = JSONObject.parseObject(report, MissionReportTb.class);exportReport(missionReportTb);}}

2、根据模板生成pdf文件并导出

首先你的制作一个pdf模板:

1.先用word做出模板界面

2.文件另存为pdf格式文件

3.通过Adobe Acrobat pro软件打开刚刚用word转换成的pdf文件(注:如果没有这个软件可以通过我的百度云下载,链接:http://pan.baidu.com/s/1pL2klzt)如果无法下载可以Google一下。

4.点击右边的"准备表单"按钮,选择"测试.pdf"选择开始

进去到编辑页面,打开后它会自动侦测并命名表单域,右键表单域,点击属性,出现文本域属性对话框(其实无需任何操作,一般情况下不需要修改什么东西,至少我没有修改哦。如果你想修改fill1等信息,可以进行修改)

5.做完上面的工作后,直接"另存为"将pdf存储就可以

以上部分是制作pdf模板操作,上述完成后,就开始通过程序来根据pdf模板生成pdf文件了,上java程序:

public class Snippet {
// 利用模板生成pdfpublic static void fillTemplate() {
// 模板路径String templatePath = "E:/测试3.pdf";
// 生成的新文件路径String newPDFPath = "E:/ceshi.pdf";PdfReader reader;FileOutputStream out;ByteArrayOutputStream bos;PdfStamper stamper;try {out = new FileOutputStream(newPDFPath);// 输出流reader = new PdfReader(templatePath);// 读取pdf模板bos = new ByteArrayOutputStream();stamper = new PdfStamper(reader, bos);AcroFields form = stamper.getAcroFields();String[] str = {"123456789", "TOP__ONE", "男", "1991-01-01", "130222111133338888", "河北省保定市"};int i = 0;java.util.Iterator<String> it = form.getFields().keySet().iterator();while (it.hasNext()) {String name = it.next().toString();System.out.println(name);form.setField(name, str[i++]);}stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为truestamper.close();Document doc = new Document();PdfCopy copy = new PdfCopy(doc, out);doc.open();PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);copy.addPage(importPage);doc.close();} catch (IOException e) {System.out.println(1);} catch (DocumentException e) {System.out.println(2);}}public static void main(String[] args) {fillTemplate();}
}

运行结果如下

更多操作

1、页面大小,页面背景色,页边空白,Title,Author,Subject,Keywords

核心代码:

//页面大小
Rectangle rect = new Rectangle(PageSize.B5.rotate());
//页面背景色
rect.setBackgroundColor(BaseColor.ORANGE);  Document doc = new Document(rect);  PdfWriter writer = PdfWriter.getInstance(doc, out);  //PDF版本(默认1.4)
writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);  //文档属性
doc.addTitle("Title@sample");
doc.addAuthor("Author@rensanning");
doc.addSubject("Subject@iText sample");
doc.addKeywords("Keywords@iText");
doc.addCreator("Creator@iText");  //页边空白
doc.setMargins(10, 20, 30, 40);  doc.open();
doc.add(new Paragraph("Hello World"));

输出结果:

2、设置密码

核心代码:

PdfWriter writer = PdfWriter.getInstance(doc, out);  // 设置密码为:"World"
writer.setEncryption("Hello".getBytes(), "World".getBytes(),  PdfWriter.ALLOW_SCREENREADERS,  PdfWriter.STANDARD_ENCRYPTION_128);  doc.open();
doc.add(new Paragraph("Hello World"));

输出结果:

3、添加Page
核心代码:

document.open();
document.add(new Paragraph("First page"));
document.add(new Paragraph(Document.getVersion()));  document.newPage();
writer.setPageEmpty(false);  document.newPage();
document.add(new Paragraph("New page"));

4、添加水印(背景图)

//图片水印
PdfReader reader = new PdfReader(FILE_DIR + "setWatermark.pdf");
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR  + "setWatermark2.pdf"));  Image img = Image.getInstance("resource/watermark.jpg");
img.setAbsolutePosition(200, 400);
PdfContentByte under = stamp.getUnderContent(1);
under.addImage(img);  //文字水印
PdfContentByte over = stamp.getOverContent(2);
over.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,  BaseFont.EMBEDDED);
over.setFontAndSize(bf, 18);
over.setTextMatrix(30, 30);
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
over.endText();  //背景图
Image img2 = Image.getInstance("resource/test.jpg");
img2.setAbsolutePosition(0, 0);
PdfContentByte under2 = stamp.getUnderContent(3);
under2.addImage(img2);  stamp.close();
reader.close();

5、插入Chunk, Phrase, Paragraph, List
核心代码

//Chunk对象: a String, a Font, and some attributes
document.add(new Chunk("China"));
document.add(new Chunk(" "));
Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
Chunk id = new Chunk("chinese", font);
id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
id.setTextRise(6);
document.add(id);
document.add(Chunk.NEWLINE);  document.add(new Chunk("Japan"));
document.add(new Chunk(" "));
Font font2 = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
Chunk id2 = new Chunk("japanese", font2);
id2.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
id2.setTextRise(6);
id2.setUnderline(0.2f, -2f);
document.add(id2);
document.add(Chunk.NEWLINE);  //Phrase对象: a List of Chunks with leading
document.newPage();
document.add(new Phrase("Phrase page"));  Phrase director = new Phrase();
Chunk name = new Chunk("China");
name.setUnderline(0.2f, -2f);
director.add(name);
director.add(new Chunk(","));
director.add(new Chunk(" "));
director.add(new Chunk("chinese"));
director.setLeading(24);
document.add(director);  Phrase director2 = new Phrase();
Chunk name2 = new Chunk("Japan");
name2.setUnderline(0.2f, -2f);
director2.add(name2);
director2.add(new Chunk(","));
director2.add(new Chunk(" "));
director2.add(new Chunk("japanese"));
director2.setLeading(24);
document.add(director2);  //Paragraph对象: a Phrase with extra properties and a newline
document.newPage();
document.add(new Paragraph("Paragraph page"));  Paragraph info = new Paragraph();
info.add(new Chunk("China "));
info.add(new Chunk("chinese"));
info.add(Chunk.NEWLINE);
info.add(new Phrase("Japan "));
info.add(new Phrase("japanese"));
document.add(info);  //List对象: a sequence of Paragraphs called ListItem
document.newPage();
List list = new List(List.ORDERED);
for (int i = 0; i < 10; i++) {  ListItem item = new ListItem(String.format("%s: %d movies",  "country" + (i + 1), (i + 1) * 100), new Font(  Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE));  List movielist = new List(List.ORDERED, List.ALPHABETICAL);  movielist.setLowercase(List.LOWERCASE);  for (int j = 0; j < 5; j++) {  ListItem movieitem = new ListItem("Title" + (j + 1));  List directorlist = new List(List.UNORDERED);  for (int k = 0; k < 3; k++) {  directorlist.add(String.format("%s, %s", "Name1" + (k + 1),  "Name2" + (k + 1)));  }  movieitem.add(directorlist);  movielist.add(movieitem);  }  item.add(movielist);  list.add(item);
}
document.add(list);

6、插入表格

PdfPTable table = new PdfPTable(3);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");  document.add(table);  

7、表格嵌套

PdfPTable table = new PdfPTable(4);  //1行2列
PdfPTable nested1 = new PdfPTable(2);
nested1.addCell("1.1");
nested1.addCell("1.2");  //2行1列
PdfPTable nested2 = new PdfPTable(1);
nested2.addCell("2.1");
nested2.addCell("2.2");  //将表格插入到指定位置
for (int k = 0; k < 24; ++k) {  if (k == 1) {  table.addCell(nested1);  } else if (k == 20) {  table.addCell(nested2);  } else {  table.addCell("cell " + k);  }
}  document.add(table);

8、设置表头

String[] bogusData = { "M0065920", "SL", "FR86000P", "PCGOLD",  "119000", "96 06", "2001-08-13", "4350", "6011648299",  "FLFLMTGP", "153", "119000.00" };
int NumColumns = 12;
// 12
PdfPTable datatable = new PdfPTable(NumColumns);
int headerwidths[] = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; // percentage
datatable.setWidths(headerwidths);
datatable.setWidthPercentage(100);
datatable.getDefaultCell().setPadding(3);
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);  datatable.addCell("Clock #");
datatable.addCell("Trans Type");
datatable.addCell("Cusip");
datatable.addCell("Long Name");
datatable.addCell("Quantity");
datatable.addCell("Fraction Price");
datatable.addCell("Settle Date");
datatable.addCell("Portfolio");
datatable.addCell("ADP Number");
datatable.addCell("Account ID");
datatable.addCell("Reg Rep ID");
datatable.addCell("Amt To Go ");  datatable.setHeaderRows(1);  //边框
datatable.getDefaultCell().setBorderWidth(1);  //背景色
for (int i = 1; i < 1000; i++) {  for (int x = 0; x < NumColumns; x++) {  datatable.addCell(bogusData[x]);  }
}  document.add(datatable);

篇幅有限,如果你需要更多操作,可以参考文章:
https://www.cnblogs.com/liaojie970/p/7132475.html

最后

如果对 Java、大数据感兴趣请长按二维码关注一波,我会努力带给你们价值。觉得对你哪怕有一丁点帮助的请帮忙点个赞或者转发哦。
关注公众号**【爱编码】,回复2019**有相关资料哦。

Springboot输出PDF文件相关推荐

  1. Rstudio Markdown中文输出PDF文件

    注意首先安装R的rticles文档模板包 Rstudio Markdown中文输出PDF文件:需要下载安装Tex文档编译器(texlive 或MikTex), 也可以运行以下代码,安装谢益辉提供的代码 ...

  2. 标签打印软件输出PDF文件过大时如何解决?

    在用标签打印软件制作标签输出PDF文件时,如果数据过多可能会出现输出的PDF文件过大,这种情况如何解决呢?在标签打印软件中,输出PDF文件时可以对PDF文件进行压缩和拆分,压缩方式有六种,分别为:默认 ...

  3. WinCC变量归档的历史数据查询结果输出PDF文件的一个方法

    提示:本文不涉及具体VBS实现代码,因为这些代码在网上可随意找到(不要只做一个伸手党),本文仅提供初学者一个思路来实现历史数据查询结果直接输出PDF. 曾经看过其他软件可以提供用户历史数据查询生成后直 ...

  4. 通过安装虚拟打印机输出PDF文件

    前两天,用FastReport输出pdf文件,在查资料时候,无意看到可通过安装一个虚拟的打印机,把任何支持打印的软件的输出,都输出为pdf文件.比如把word,excel...等等都可以转化成pdf文 ...

  5. cad怎么输出pdf文件?

    因为工作的需要,我们经常要把AutoCAD图转换PDF文件,网上有很多的方法,大多数都是麻烦的,CAD怎么输出PDF文件呢?下面我就来简单的介绍一下. 第一步:在百度浏览器搜搜迅捷caj转换器,然后找 ...

  6. SpringBoot 生成pdf文件(含报表)

    使用 iText 导出pdf表格 iText 是一种生成PDF报表的Java组件,其maven依赖如下: <dependency><groupId>com.itextpdf&l ...

  7. 如何将CSDN文档输出PDF文件?

    简 介: 根据生成文档的需要,在CSDN上寻找一些介绍将MARKDOWN文档生成PDF博文.根据他们介绍的方法,测试打印的效果.特别是对于CSDN新增加的一些显示元素的清理,可以生成更加干净完整的PD ...

  8. 【VBA研究】输出PDF文件合并时出错

    作者:iamlaosong 在做一个打印通知单的工具时(这个工具已经做过很多个,技术基本一样),输出的多个PDF文件合并时出错,单个PDF文件打开没有问题. 合并其他文件没有问题,那问题只能是这些单个 ...

  9. 输出pdf文件的一个简单方法

    最近尝试使用pdflib输出pdf文档,但是pdflib的方法很多,要将打印输出与pdf匹配,需要编写很多代码,而且一旦需要修改,也是个麻烦事,所以就想了个偷懒的办法,如下: 1.在项目中加入pdfl ...

最新文章

  1. 猫猫学iOS 之第一次打开Xcode_git配置,git简单学习
  2. .NET开发不可不知、不可不用的辅助类(三)(报表导出---终结版)
  3. 请问SAP PLM与WINDCHILL比优势在哪里?
  4. 不做“浮冰”,深挖AI技术和场景
  5. 对示波器测量正弦波幅值和相位仿真实验
  6. 安装php出现 “make: *** [ext/gd/libgd/gd_jpeg.lo] Error ”
  7. P4016 负载平衡问题(最小费用最大流)
  8. xpe低配置系统解决“写缓存失败”问题
  9. 干货!286页李宏毅《深度学习讲义》
  10. TCP和UDP,HTTP和HTTPS
  11. Git详解之二 Git基础(第二部分)
  12. 隐马尔科夫-维特比算法
  13. 数据库关于group by 两个或以上条件的分析
  14. Fastjson 远程命令执⾏漏洞
  15. LTE网络架构 学习整理
  16. 水环境指标 中文对照
  17. Linux中RAID与LVM磁盘列阵技术的使用
  18. 网易2019:矩形重叠
  19. ssh开启图形界面_分享|3 个 Linux 上的 SSH 图形界面工具
  20. 内部矩阵维度必须一致simulink_手把手教你将矩阵画成张量网络图

热门文章

  1. c语言学好了可以学啥,学好c语言可以干什么?
  2. springboot集成mybatis+Generator代码生成
  3. 服装收银软件排行榜新鲜出炉,码住这一篇就够了!
  4. deepin15.9.1 WIFI使用提速
  5. php redis 制作高迸发秒杀
  6. 【旧资料整理】解决firefox3迅雷插件右键查看页面源代码无效问题
  7. A Birthday Party
  8. 大学毕业,应该去大公司?还是小公司?
  9. JS大总结javascript事件查询综合
  10. 做教育的人,做基业长青的事情