根据PDF模版填充数据并生成新的PDF

准备模版

  1. 使用 福昕高级PDF编辑器(本人用的这个,其他的也行,能作模版就行)
  2. 打开PDF文件点击 表单 选项,点击 文本域
  3. 在需要填充数据的位置设计文本域
  4. 设置 名称、提示
  5. 名称相当于 属性名,提示就是提示,说明这个是什么

导入依赖

XML 复制代码
        <!--itext-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.13</version>
        </dependency>

函数编写

generatePdf

放 ServiceIMpl 里就行,这个是直接浏览器下载生成后的附件

java 复制代码
    private final HttpServletResponse response;

    public PdfServiceImpl(HttpServletResponse response) {
        this.response = response;
    }
public void generatePdf(Map<String, String> params) {
        // 读取资源文件夹下的模板
        ClassPathResource resource = new ClassPathResource("pdf-template/文件.pdf");
        InputStream inputStream = resource.getInputStream();

        PdfReader reader = null;
        ByteArrayOutputStream bos = null;
        try {
            reader = new PdfReader(inputStream);
            bos = new ByteArrayOutputStream();
            PdfStamper pdfStamper = new PdfStamper(reader, bos);
            AcroFields acroFields = pdfStamper.getAcroFields();

            // 字体设置
            BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            for (Map.Entry<String, String> param : params.entrySet()) {
                // 设置文本域的字体为中文字体
                acroFields.setFieldProperty(param.getKey(), "textFont", font, null);
                // 将 map 中的值写到 pdf 模板对应的文本域中
                acroFields.setField(param.getKey(), param.getValue());
            }

            // 如果为false那么生成的PDF文件还能编辑,所以一定要设为true
            pdfStamper.setFormFlattening(true);
            pdfStamper.close();

            // 返回文件
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(bos.toByteArray());
            ServletUtils.writeAttachment(response, "新文件.pdf", bos.toByteArray());
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        } finally {
            try {
                assert bos != null;
                bos.close();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

工具类

ServletUtils

java 复制代码
import cn.hutool.core.io.IoUtil;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Package_Name com.lesliecheung.javacase.util.pdf
 * @Author Leslie Lee
 * @TIME
 * @Version
 */
public class ServletUtils {
    /**
     * 返回附件
     *
     * @param response 响应
     * @param filename 文件名
     * @param content  附件内容
     */
    public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException {
        // 设置 header 和 contentType
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        // 输出附件
        IoUtil.write(response.getOutputStream(), false, content);
    }
}

获取模版 inputStream流

从url下载文件

java 复制代码
            // 获取文件地址
            String urlPath = "模板资源文件链接-url";
            // 下载文件
            URL url = new URL(urlPath);
            URLConnection connection = url.openConnection();
            // 设置请求超时时长为 5 秒
            connection.setConnectTimeout(5*1000);
            // 读取数据
            InputStream inputStream = connection.getInputStream();

从某路径下直接取

java 复制代码
        String urlPath = "D:\\文件.pdf";
        File file1 = new File(urlPath);
        InputStream inputStream = new FileInputStream(file1);

新文件写入其他位置,操作 **bos.toByteArray()**就好了

java 复制代码
            File file = new File("D:/新文件.pdf");
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream boss = new BufferedOutputStream(fos);
            boss.write(bos.toByteArray());

            fos.close();
            boss.close();
            System.out.println("成了");

Leslie Lee 随笔

相关推荐
大胖爱学习_MJ27 分钟前
超越向量检索:用 Spring Boot + Neo4j 手搓 GraphRAG 知识图谱记忆
java·后端·python
吃我一窝jio41 分钟前
Java博客极简写作三支柱
java
知识搬运临时工44 分钟前
双路服务器跑得比单路慢:JVM 没看见内存的距离
java·后端
吃我一窝jio1 小时前
Java Bug复盘与性能优化实战
java·开发语言·笔记
Flynn Liang2 小时前
Java 序列化也能成为瓶颈:Jackson、Protobuf 与 Redis 序列化的生产实践
java·redis·性能优化·jackson·序列化·protobuf
Yeats_Liao2 小时前
物联网平台优化系列(三):全局懒加载吞掉 MQTT 初始化
java·物联网·struts
千寻技术帮2 小时前
基于Spring Boot+Vue的农产品销售管理系统(源码+文档+数据库,附部署教程)
java·数据库·spring boot·后端·mysql·vue
好用斋2 小时前
拾光坞G2使用新版Docker懒人包:一键安装、一键升级、一键卸载
java·docker·容器
码云社区2 小时前
Spring Boot陪诊陪护系统源码:医院目录、服务项目与城市业务配置
java·spring boot·后端·陪诊陪护·医院陪诊·医院陪护·陪护小程序
码云社区3 小时前
Spring Boot云快充协议源码:充电桩远程重启、参数配置与升级运维方案
运维·spring boot·后端·充电桩·充电系统·充电软件