Java根据PDF表单模板和CSV表格批量生成证书等文件

最近遇到需求,需要批量生成800+个证书,所以写了一个简单个工具,原理就是PDF表单,读取csv表格中的数据,然后批量生成,比较通用的一个工具,所以分享一下

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.*;
import java.util.*;

/**
 * 生成证书
 * 将表格另存为UTF-8编码的csv文件,若为其他编码请修改常量CHARSET_NAME,修改对应表头行数为表格对应表头行数运行即可,Windows系统的文件路径为C:\\User\\xxx\\xxx.csv格式,linux和Unix系统为/格式
 *
 * @author xuwei
 */
public class Main {

    /**
     * 设置文件编码格式,不对应的话会统计错误
     */
    private static final String CHARSET_NAME = "UTF-8";

    /**
     * 表格路径
     */
    private static final String CSV_FILE_OF_USER = "/Users/xuwei/Desktop/users.csv";

    /**
     * 证书模板路径
     */
    private static final String CERTIFICATE_TEMPLATE = "/Users/xuwei/Desktop/结业证书第三季.pdf";

    /**
     * 输出文件夹路径
     */
    private static final String PATH_OF_OUTPUT = "/Users/xuwei/Desktop/certificate/";

    /**
     * 输出文件名对应表头字段名
     */
    private static final String FILE_NAME_OF_OUTPUT = "姓名";

    /**
     * 字体路径
     */
    private static final String PATH_OF_FONT = "KaiTi.ttc,1";

    /**
     * 主函数
     *
     * @param args
     */
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(new File(CSV_FILE_OF_USER), CHARSET_NAME)) {
            //读取表头
            String[] header = scanner.nextLine().split(",");
            while (scanner.hasNextLine()) {
                String[] fields = scanner.nextLine().split(",");
                String fileName = new Date().toString();
                Map<String, String> data = new HashMap<>(header.length);
                for (int i = 0; i < header.length && i < fields.length; ++i) {
                    if(FILE_NAME_OF_OUTPUT.equals(deleteUtf8Bom(header[i]))){
                        fileName = fields[i];
                    }
                    data.put(deleteUtf8Bom(header[i]), deleteUtf8Bom(fields[i]));
                }
                System.out.println("准备生成"+fileName+"的证书");
                generateCertificate(data,PATH_OF_OUTPUT+"/"+fileName+".pdf");
            }
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成证书
     * @param fields
     * @param fileName
     */
    public static void generateCertificate(Map<String,String> fields,String fileName) throws IOException, DocumentException {
        //读取模板文件
        PdfReader pdfReader = new PdfReader(CERTIFICATE_TEMPLATE);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        //读取PDF模板内容
        PdfStamper pdfStamper = new PdfStamper(pdfReader, byteArrayOutputStream);
        pdfStamper.getUnderContent(1);

        //使用项目下的自定义的中文字体
        BaseFont baseFont = BaseFont.createFont(PATH_OF_FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        //获取模板中的所有字段
        AcroFields acroFields = pdfStamper.getAcroFields();
        acroFields.addSubstitutionFont(baseFont);
        for (String key : fields.keySet()){
            acroFields.setField(key,fields.get(key));
        }
        //必须要调用这个,否则文档不会生成的
        pdfStamper.setFormFlattening(true);
        pdfStamper.close();
        //将要生成的目标PDF文件名称
        OutputStream fileOutputStream = new FileOutputStream(fileName);
        fileOutputStream.write(byteArrayOutputStream.toByteArray());
        fileOutputStream.flush();
        fileOutputStream.close();
        byteArrayOutputStream.close();
    }

    /**
     * 解决utf8字符串中有隐含字符问题
     * @param str
     * @return
     */
    public static String deleteUtf8Bom(String str) {
        byte[] utf8BomBytes = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
        byte[] bytes = str.getBytes();
        if (bytes[0] == utf8BomBytes[0]
                && bytes[1] == utf8BomBytes[1]
                && bytes[2] == utf8BomBytes[2]) {
            return new String(bytes, 3, bytes.length - 3);
        }
        return str;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeXu_cyber

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值