java 生成pdf文件(易上手版)

1.引入相关pom依赖

<!--pdf start-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.itextpdf/font-asian -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>font-asian</artifactId>
    <version>7.1.2</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>
<!--pdf end-->

2.准备pdf 模板文件

准备表单 填写对应java字段

3.java生成(初步未优化版)

@GetMapping(value = "/makePdf")
    public AdusResponse makePdf() {
        Map<String,Object> params = new HashMap<>();
        params.put("id",298);
        ReliefBillEntity entity = reliefBillFacade.selectReliefBillEntity(params);
        // 模板路径
        String templatePath = "C:\\Users\\Administrator\\Desktop\\test.pdf";
        // 生成的新文件路径
        String newPDFPath = "C:\\Users\\Administrator\\Desktop\\b.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();

            form.setField("reliefNumber",entity.getReliefNumber());
            form.setField("supplierId",entity.getSupplierId().toString());
            form.setField("reliefDate",entity.getReliefDate().toString());
            form.setField("accPeriod",entity.getAccPeriod());
            form.setField("orderNumber",entity.getReliefNumber());
            form.setField("SKC",entity.getAccPeriod());
            form.setField("chksheetId",entity.getChksheetId().toString());
            form.setField("reliefType",entity.getReliefType().getDesc());
            form.setField("oneType",entity.getOneType());
            form.setField("twoType",entity.getOneType());
            form.setField("reliefTypeDesc",entity.getReliefType().getDesc());
            form.setField("number",entity.getOneType());
            form.setField("price",entity.getOneType());
            form.setField("reliefMoney",entity.getReliefMoney().toString());
            form.setField("curCode",entity.getCurCode());
            form.setField("auditor",entity.getAccPeriod());
            form.setField("addTime",entity.getAddTime().toString());
            form.setField("auditor2",entity.getAccPeriod());

            stamper.setFormFlattening(false);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
            stamper.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 (Exception e) {
            System.out.println(1);
        }
        return new AdusResponse(SysConstants.ResponseCode.SUCCESS, "", null);

    }

最后提供模板文件与生成后的pdf文件截图

具体项目中需要注意资源的关闭,统计生成的文件可以放置文件服务器上统一提供链接地址进行下载等等。这里仅仅初步更具模板生成含数据的pdf文件。

这种方式的缺点:不够灵活,模板文件较为死板,不便于拓展。优点:能快速更具模板文件实现功能,难度低

posted @ 2019-06-13 13:25  小白个人成长记  阅读(1560)  评论(0编辑  收藏  举报