java根据pdf模板生成pdf

红太狼 2024-03-17 23:38 273阅读 0赞
  1. import com.hw.common.exception.BaseException;
  2. import com.itextpdf.text.Document;
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.pdf.*;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.util.Map;
  10. /**
  11. * @Description TODO
  12. * @Author liqinglong
  13. * @DateTime 2022-12-06 09:58
  14. * @Version 1.0
  15. */
  16. public class PdfUtils {
  17. /**
  18. * 根据模板创建生成pdf
  19. * @param map 模板中的表单数据 key-表单属性值;value-值
  20. * @param templatePath 模板路径
  21. * @return 返回生成的pdf文件路径
  22. */
  23. public static String createPdfByTemplate(Map<String,Object> map, String templatePath,String newPdfPath) {
  24. PdfReader reader;
  25. ByteArrayOutputStream bos;
  26. PdfStamper stamper;
  27. //生成的pdf文件存放地址 要确保文件夹的存在
  28. newPdfPath = newPdfPath + (System.currentTimeMillis()) +".pdf";
  29. System.out.println(newPdfPath);
  30. try {
  31. //设置字体是必须要的,不然没法向模板pdf里写值
  32. //用以支持中文
  33. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  34. // 读取pdf模板
  35. reader = new PdfReader(templatePath);
  36. bos = new ByteArrayOutputStream();
  37. stamper = new PdfStamper(reader, bos);
  38. //拿到pdf模板中的表单属性
  39. AcroFields form = stamper.getAcroFields();
  40. //设置字体
  41. form.addSubstitutionFont(bfChinese);
  42. java.util.Iterator<String> it = form.getFields().keySet().iterator();
  43. //遍历表单属性,对每个属性赋值
  44. while (it.hasNext()) {
  45. String name = it.next().toString();
  46. Object o = map.get(name);
  47. String value = o == null ? null : o.toString();
  48. if(value == null){
  49. continue;
  50. }
  51. form.setField(name,value);
  52. }
  53. // 如果为false那么生成的PDF文件还能编辑,一定要设为true
  54. stamper.setFormFlattening(true);
  55. bos.close();
  56. stamper.close();
  57. Document doc = new Document();
  58. File file = new File(newPdfPath);
  59. PdfCopy copy = new PdfCopy(doc, new FileOutputStream(file));
  60. doc.open();
  61. PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
  62. copy.addPage(importPage);
  63. doc.close();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. throw new BaseException("生成pdf失败-IOException,请联系管理员");
  67. } catch (DocumentException e) {
  68. throw new BaseException("生成pdf失败-DocumentException,请联系管理员");
  69. }
  70. return newPdfPath;
  71. }
  72. public static void main(String[] args) {
  73. FilingPropertiesVo filingVo = new FilingPropertiesVo();
  74. filingVo.setSaleRegistNo("123456789");
  75. filingVo.setCustomerType(1);
  76. filingVo.setRefereeName("张三");
  77. filingVo.setRefereePhone("13809449028");
  78. filingVo.setRefereeDept("酒业公司");
  79. filingVo.setRefereePost("销售经理");
  80. filingVo.setPersonContactName("李四");
  81. filingVo.setPersonContactPhone("13809449026");
  82. filingVo.setPersonContactNumber("522228198610071613");
  83. filingVo.setMatName("茅台纪念酒1951|酱香型|53°|500ML|2017");
  84. filingVo.setAmount(10);
  85. filingVo.setMatCode("20221026");
  86. filingVo.setMatName1("飞天茅台|53°|500ML|2015|酱香型");
  87. filingVo.setAmount1(15);
  88. filingVo.setMatCode1("12345678901001");
  89. filingVo.setMatName2("白酒全家福|酱香型|53°|500ML|2022");
  90. filingVo.setAmount2(20);
  91. filingVo.setMatCode2("20221027");
  92. filingVo.setDeliveryAddress("贵州省贵阳市南明区朝阳洞路50号");
  93. Map<String,Object> dataMap = JSON.parseObject(JSON.toJSONString(filingVo));
  94. String path = "E:/project/airport/airport_service/airport_admin/src/main/resources/file/template/pdf/";
  95. String filePath = path + "registration_form.pdf";
  96. PdfUtils.createPdfByTemplate(dataMap,filePath,path);
  97. }
  98. }

发表评论

表情:
评论列表 (有 0 条评论,273人围观)

还没有评论,来说两句吧...

相关阅读