java(itextpdf)根据不同实体类后台打印对应pdf(与thymeleaf或者freemarker不同的是:不需要写html,css等前端内容)

这篇博客介绍了如何在Java中利用iTextPDF库,根据不同的实体类动态生成PDF文件。通过实体类的注解获取字段信息,将数据填充到预先设计的PDF模板中,避免了编写HTML和CSS。文章提供了详细步骤,包括创建模板、设置占位符、反射获取实体类信息以及程序中处理字体格式等。
摘要由CSDN通过智能技术生成

 参考资料:www.cnblogs.com/wangpeng00700/p/8418594.html

 概括:

根据不同的流程实体类并且通过在实体类字段上加上注解,通过反射拿到实体类的注解属性和属性值,通过属性值判断是什么类型的数据,依次给pdf模板中的不同类型的占位符赋值,打印此流程的pdf。

所需依赖:

<dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.4.3</version>
</dependency>   

上代码:

package com.example.demo.utils;

/**
 * @ClassName:PDFUtils
 * @Package:com.example.demo.utils
 * @Description: com.example.demo.utils.PDFUtils:
 * @date:2022/2/24 10:27
 * @author:bsm
 */

import com.example.demo.PDFFill;
import com.example.demo.TestPojo;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class PdfUtils {
    public static String toPDF(int flowType, Object data) {
        Map<String, String> datamap = new HashMap();
        Map<String, String> imgmap = new HashMap();
        Map<String, Object> resultMap = new HashMap();
        String templatePath = "";
        String newPDFPath = "";
        if(System.getProperty("os.name").toLowerCase().contains("win")){
            templatePath = "E:\\shengtingPDF\\src\\main\\resources\\pdflocation\\"+ flowType +".pdf";
            newPDFPath = "E:\\shengtingPDF\\src\\main\\resources\\pdflocation\\"+ flowType +"out.pdf";
        }else{
            templatePath = "/home/pdflocation/"+ flowType +".pdf";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值