当前位置: 首页 > news >正文

使用IText导出复杂pdf

1、问题描述 

        需要将发票导出成pdf,要求每页都必须包含发票信息和表头行。

 

 

2、解决方法

        使用IText工具实现PDF导出

        IText8文档:Examples (itextpdf.com)

 

3、我的代码

      

 

 

        引入Itext依赖,我这里用的是8.0.1版本

 <dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>8.0.1</version><type>pom</type></dependency>
MyItextpdfUtils.java
package com.easyexcel.util;import com.easyexcel.handler.PaginationEventHandler;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.properties.AreaBreakType;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** @author Wulc* @date 2023/8/10 17:08* @description*/
@Component
public class MyItextpdfUtils {public void createPDF() throws java.io.IOException {Resource resource = new ClassPathResource("/");String path = resource.getFile().getPath();//设置中文字体 C:\Windows\Fonts//PdfFont chineseFont =getFont();//PdfFont chineseFont = PdfFontFactory.createFont(this.getClass().getClassLoader().getResource("simsun.ttf").getPath());PdfFont chineseFontForTemplate = PdfFontFactory.createFont("D:\\学习资料\\后端\\STSONG.TTF");PdfFont chineseFontForContent = PdfFontFactory.createFont("D:\\学习资料\\后端\\STSONG.TTF");//创建每页的共有模板//*********************每页的共有模板*********************************String templatePath = path + "\\template.pdf";PdfDocument pdfDocumentTemplate = new PdfDocument(new PdfWriter(templatePath));//Document documentTemplate = new Document(pdfDocumentTemplate, PageSize.A4).setFont(chineseFontForTemplate);Document documentTemplate = new Document(pdfDocumentTemplate, PageSize.A4);//插入logo图片Table logoTemplateTable = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth().setBorder(Border.NO_BORDER);ImageData imageData = ImageDataFactory.create(this.getClass().getClassLoader().getResource("logo.png"));Image image = new Image(imageData);image.setHeight(50);image.setWidth(100);logoTemplateTable.addCell(new Cell().setBorder(Border.NO_BORDER).add(image));//插入logo图片下方的一些信息Table logoInfoTable = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth().setBorder(Border.NO_BORDER);logoInfoTable.addCell(new Cell().setBorder(Border.NO_BORDER).setPadding(1).setFontSize(10).add(new Paragraph("Description1")));logoInfoTable.addCell(new Cell().setBorder(Border.NO_BORDER).setPadding(1).setFontSize(10).add(new Paragraph("Description2")));logoInfoTable.addCell(new Cell().setBorder(Border.NO_BORDER).setPadding(1).setFontSize(10).add(new Paragraph("Description3")));//插入标题Table titleTable = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth().setBorder(Border.NO_BORDER);titleTable.addCell(new Cell(1, 4).setBorder(Border.NO_BORDER).setPadding(1).setFontSize(15).add(new Paragraph("TITLE")).setTextAlignment(TextAlignment.CENTER));//插入标题下的一些信息Table titleInfoTable = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth();titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionA")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerA")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionB")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerB")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionC")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerC")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionD")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerD")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionE")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerE")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionF")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerF")));documentTemplate.add(logoTemplateTable);documentTemplate.add(logoInfoTable);documentTemplate.add(titleTable);documentTemplate.add(titleInfoTable);//*********************每页的共有模板*********************************//*********************每页的内容************************************String contentPath = path + "\\content.pdf";PdfDocument pdfDocumentContent = new PdfDocument(new PdfWriter(contentPath));//把内容使用共有模板pdfDocumentContent.addEventHandler(PdfDocumentEvent.END_PAGE, new PaginationEventHandler(pdfDocumentTemplate.getFirstPage().copyAsFormXObject(pdfDocumentContent)));Document documentContent = new Document(pdfDocumentContent, PageSize.A4).setFont(chineseFontForContent);//每页的content距离上面的template的距离documentContent.setTopMargin(250);Table contentTable = new Table(UnitValue.createPercentArray(6)).useAllAvailableWidth();//插入清单表格标题contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("No")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title1")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title2")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title3")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title4")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title5")));for (int i = 0; i < 300; i++) {contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph(String.valueOf(i))));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content1")));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content2")));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content3")));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content4")));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content5")));}//尾页Table lastInfoTable = new Table(UnitValue.createPercentArray(3)).setWidth(300);lastInfoTable.addCell(new Cell(1, 3).setPadding(1).setFontSize(8).add(new Paragraph("Total:")));lastInfoTable.addCell(new Cell(1, 1).setPadding(1).setFontSize(8).add(new Paragraph("统计A:")));lastInfoTable.addCell(new Cell(1, 2).setPadding(1).setFontSize(8).add(new Paragraph("1234567")));lastInfoTable.addCell(new Cell(1, 1).setPadding(1).setFontSize(8).add(new Paragraph("统计B:")));lastInfoTable.addCell(new Cell(1, 2).setPadding(1).setFontSize(8).add(new Paragraph("7654321")));//*********************每页的内容************************************documentContent.add(contentTable);//尾页新开一页documentContent.add(new AreaBreak(AreaBreakType.NEXT_PAGE));documentContent.add(lastInfoTable);documentTemplate.close();documentContent.close();}}

 

PDFTest.java​​​​​​​
package com.easyexcel;import com.easyexcel.util.MyItextpdfUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;/*** @author Wulc* @date 2023/8/10 17:52* @description*/
@SpringBootTest(classes = SpringbootApplication.class)
@RunWith(SpringRunner.class)
public class PDFTest {@Autowiredprivate MyItextpdfUtils myItextpdfUtils;@Testpublic void test6() throws IOException {myItextpdfUtils.createPDF();}
}

        测试一下:

 

4、总结

        IText8不支持中文,需要引入外部字体文件,如果是以其中一个pdf作为每页的背景模板生成PDF这种方式(copyAsFormXObject),它只能支持其中一个pdf中文,另一个就不支持了。

Document documentTemplate = new Document(pdfDocumentTemplate, PageSize.A4).setFont(chineseFontForTemplate);
Document documentContent = new Document(pdfDocumentContent, PageSize.A4).setFont(chineseFontForContent);

如上代码,虽然我同时把背景版和内容同时都设置了中文字体,但是template和content合一块的时候,template的背景版pdf的中文字体就会失效了。

         不过还好,因为是海外的发票都是英文的,因此不需要考虑支持中文的问题。

        希望哪位大佬能帮忙解决一下IText8 copyAsFormXObject中文兼容性问题!!!

5、参考资料

https://www.cnblogs.com/sky-chen/p/13026203.html#autoid-1-4-5-0-0-0
https://kb.itextpdf.com/home/it7kb/examples/repeating-parts-of-a-form
https://zhuanlan.zhihu.com/p/537723847
https://blog.csdn.net/weixin_43409994/article/details/118157694
https://blog.csdn.net/u012397189/article/details/126345744
https://blog.csdn.net/Thinkingcao/article/details/84988392

 

 

http://www.lryc.cn/news/130892.html

相关文章:

  • 多线程并发服务器(TCP)
  • uni-app的Vue.js实现微信小程序的紧急事件登记页面功能
  • 面试题 17.16.按摩师
  • vscode里配置C#环境并运行.cs文件
  • uniapp配置添加阿里巴巴图标icon流程步骤
  • 大模型基础02:GPT家族与提示学习
  • 算法基础课——基础算法(模板整理)
  • 如何解决使用npm出现Cannot find module ‘XXX\node_modules\npm\bin\npm-cli.js’错误
  • 【华为认证数通高级证书实验-分享篇2】
  • ui设计需要学编程吗难不难学习 优漫动游
  • 什么是线程优先级?Java中的线程优先级是如何定义和使用的?
  • 无涯教程-TensorFlow - XOR实现
  • 计算机组成与设计 Patterson Hennessy 笔记(二)MIPS 指令集
  • 【设计模式】模板方法模式(Template Method Pattern)
  • 【潮州饶平】联想 IBM x3850 x6 io主板故障 服务器维修
  • 【AIGC】 国内版聊天GPT
  • 如何在Vue中进行单元测试?什么是Vue的模块化开发?
  • Matlab编程示例3:Matlab求二次积分的编程示例
  • 【Linux】线程同步和死锁
  • Matplotlib数据可视化(二)
  • 图像去雨-雨线清除-图像处理-(计算机作业附代码)
  • pycharm调整最大堆发挥最大
  • uni-app 经验分享,从入门到离职(二)—— tabBar 底部导航栏实战基础篇
  • 【李沐】3.2线性回归从0开始实现
  • 一百五十六、Kettle——Linux上安装的Kettle9.3连接ClickHouse数据库(亲测,附流程截图)
  • 图数据库_Neo4j和SpringBoot整合使用_创建节点_删除节点_创建关系_使用CQL操作图谱---Neo4j图数据库工作笔记0009
  • Uniapp连接蓝牙设备
  • linux切换到root用户:su root和sudo su命令的区别
  • kafka-- kafka集群 架构模型职责分派讲解
  • Effective C++条款07——为多态基类声明virtual析构函数(构造/析构/赋值运算)