java根据pdf模板生成pdf


import com.hw.common.exception.BaseException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @Description TODO
 * @Author liqinglong
 * @DateTime 2022-12-06 09:58
 * @Version 1.0
 */
public class PdfUtils {
    /**
     * 根据模板创建生成pdf
     * @param map 模板中的表单数据 key-表单属性值;value-值
     * @param templatePath 模板路径
     * @return 返回生成的pdf文件路径
     */
    public static String createPdfByTemplate(Map<String,Object> map, String templatePath,String newPdfPath) {
        PdfReader reader;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        //生成的pdf文件存放地址 要确保文件夹的存在
        newPdfPath = newPdfPath + (System.currentTimeMillis()) +".pdf";
        System.out.println(newPdfPath);
        try {
            //设置字体是必须要的,不然没法向模板pdf里写值
            //用以支持中文
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // 读取pdf模板
            reader = new PdfReader(templatePath);
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            //拿到pdf模板中的表单属性
            AcroFields form = stamper.getAcroFields();
            //设置字体
            form.addSubstitutionFont(bfChinese);
            java.util.Iterator<String> it = form.getFields().keySet().iterator();
            //遍历表单属性,对每个属性赋值
            while (it.hasNext()) {
                String name = it.next().toString();
                Object o =  map.get(name);
                String value = o == null ? null : o.toString();
                if(value == null){
                    continue;
                }
                form.setField(name,value);
            }
            // 如果为false那么生成的PDF文件还能编辑,一定要设为true
            stamper.setFormFlattening(true);
            bos.close();
            stamper.close();

            Document doc = new Document();
            File file = new File(newPdfPath);
            PdfCopy copy = new PdfCopy(doc, new FileOutputStream(file));
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BaseException("生成pdf失败-IOException,请联系管理员");
        } catch (DocumentException e) {
            throw new BaseException("生成pdf失败-DocumentException,请联系管理员");
        }
        return newPdfPath;
    }

 
    public static void main(String[] args) {
        FilingPropertiesVo filingVo = new FilingPropertiesVo();
        filingVo.setSaleRegistNo("123456789");
        filingVo.setCustomerType(1);
        filingVo.setRefereeName("张三");
        filingVo.setRefereePhone("13809449028");
        filingVo.setRefereeDept("酒业公司");
        filingVo.setRefereePost("销售经理");
        filingVo.setPersonContactName("李四");
        filingVo.setPersonContactPhone("13809449026");
        filingVo.setPersonContactNumber("522228198610071613");

        filingVo.setMatName("茅台纪念酒1951|酱香型|53°|500ML|2017");
        filingVo.setAmount(10);
        filingVo.setMatCode("20221026");

        filingVo.setMatName1("飞天茅台|53°|500ML|2015|酱香型");
        filingVo.setAmount1(15);
        filingVo.setMatCode1("12345678901001");

        filingVo.setMatName2("白酒全家福|酱香型|53°|500ML|2022");
        filingVo.setAmount2(20);
        filingVo.setMatCode2("20221027");

        filingVo.setDeliveryAddress("贵州省贵阳市南明区朝阳洞路50号");


        Map<String,Object> dataMap = JSON.parseObject(JSON.toJSONString(filingVo));
        String path = "E:/project/airport/airport_service/airport_admin/src/main/resources/file/template/pdf/";
        String filePath = path + "registration_form.pdf";
        PdfUtils.createPdfByTemplate(dataMap,filePath,path);

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值