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


    准备模版

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

    导入依赖

    1. <dependency>
    2. <groupId>com.itextpdfgroupId>
    3. <artifactId>itextpdfartifactId>
    4. <version>5.4.2version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.itextpdfgroupId>
    8. <artifactId>itext-asianartifactId>
    9. <version>5.2.0version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.apache.pdfboxgroupId>
    13. <artifactId>pdfboxartifactId>
    14. <version>2.0.13version>
    15. dependency>

    函数编写

    generatePdf

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

    1. private final HttpServletResponse response;
    2. public PdfServiceImpl(HttpServletResponse response) {
    3. this.response = response;
    4. }
    5. public void generatePdf(Map params) {
    6. // 读取资源文件夹下的模板
    7. ClassPathResource resource = new ClassPathResource("pdf-template/文件.pdf");
    8. InputStream inputStream = resource.getInputStream();
    9. PdfReader reader = null;
    10. ByteArrayOutputStream bos = null;
    11. try {
    12. reader = new PdfReader(inputStream);
    13. bos = new ByteArrayOutputStream();
    14. PdfStamper pdfStamper = new PdfStamper(reader, bos);
    15. AcroFields acroFields = pdfStamper.getAcroFields();
    16. // 字体设置
    17. BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    18. for (Map.Entry param : params.entrySet()) {
    19. // 设置文本域的字体为中文字体
    20. acroFields.setFieldProperty(param.getKey(), "textFont", font, null);
    21. // 将 map 中的值写到 pdf 模板对应的文本域中
    22. acroFields.setField(param.getKey(), param.getValue());
    23. }
    24. // 如果为false那么生成的PDF文件还能编辑,所以一定要设为true
    25. pdfStamper.setFormFlattening(true);
    26. pdfStamper.close();
    27. // 返回文件
    28. ServletOutputStream outputStream = response.getOutputStream();
    29. outputStream.write(bos.toByteArray());
    30. ServletUtils.writeAttachment(response, "新文件.pdf", bos.toByteArray());
    31. } catch (IOException | DocumentException e) {
    32. e.printStackTrace();
    33. } finally {
    34. try {
    35. assert bos != null;
    36. bos.close();
    37. reader.close();
    38. } catch (IOException e) {
    39. e.printStackTrace();
    40. }
    41. }
    42. }

    工具类

    ServletUtils

    1. import cn.hutool.core.io.IoUtil;
    2. import org.springframework.http.MediaType;
    3. import javax.servlet.http.HttpServletResponse;
    4. import java.io.IOException;
    5. import java.net.URLEncoder;
    6. /**
    7. * @Package_Name com.lesliecheung.javacase.util.pdf
    8. * @Author Leslie Lee
    9. * @TIME
    10. * @Version
    11. */
    12. public class ServletUtils {
    13. /**
    14. * 返回附件
    15. *
    16. * @param response 响应
    17. * @param filename 文件名
    18. * @param content 附件内容
    19. */
    20. public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException {
    21. // 设置 header 和 contentType
    22. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
    23. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    24. // 输出附件
    25. IoUtil.write(response.getOutputStream(), false, content);
    26. }
    27. }

    获取模版 inputStream

    从url下载文件

    1. // 获取文件地址
    2. String urlPath = "模板资源文件链接-url";
    3. // 下载文件
    4. URL url = new URL(urlPath);
    5. URLConnection connection = url.openConnection();
    6. // 设置请求超时时长为 5 秒
    7. connection.setConnectTimeout(5*1000);
    8. // 读取数据
    9. InputStream inputStream = connection.getInputStream();

    从某路径下直接取

    1. String urlPath = "D:\\文件.pdf";
    2. File file1 = new File(urlPath);
    3. InputStream inputStream = new FileInputStream(file1);

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

    1. File file = new File("D:/新文件.pdf");
    2. FileOutputStream fos = new FileOutputStream(file);
    3. BufferedOutputStream boss = new BufferedOutputStream(fos);
    4. boss.write(bos.toByteArray());
    5. fos.close();
    6. boss.close();
    7. System.out.println("成了");

                                                                    Leslie Lee 随笔

  • 相关阅读:
    Redis未授权访问漏洞实战
    数据库有多少种
    DevOps(十二)Jenkins实战之Web发布到远程服务器
    LaTeX入门学习9(tikz基础-01)
    macos遇到的问题[碰到问题就更新]
    高通SDX12:Keypad按键相关(PowerKey、Reset)
    JAVA培训之数据库表关联关系
    XTTS系列之三:中转空间的选择和优化
    数分面试题2-牛客
    六、redis安装和配置
  • 原文地址:https://blog.csdn.net/Leslie_Lei/article/details/139326816