PDF为模板-填充数据,转换成图片、下载导出

先上代码

    //先读取模板
    InputStream inputStream = classLoader.getResourceAsStream("templates/acceptance-letter/print/acceptanceLetter.pdf");
    // 创建输出PDF文件 
    ByteArrayOutputStream fos = new ByteArrayOutputStream();
	
	下面是开始填充数据
    // 加载原始PDF文件
    PdfReader reader = new PdfReader(inputStream);
    // 创建PdfStamper对象,并设置文字内容和位置
    PdfStamper stamper = new PdfStamper(reader, fos, '\0', true);
    PdfContentByte content = stamper.getOverContent(1);
    BaseFont bfChinese = BaseFont.createFont("/font/simsun.ttf", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
    float size = 15;
    content.beginText();
    content.setFontAndSize(bfChinese,size);
    content.setTextMatrix(155, 355);
    content.showText(name);
    content.endText();
     // 关闭PdfStamper对象和输出流
    stamper.close();
    fos.close();
	
	/下面把上面的输出流转成图片形式
	  InputStream newInputStream = new ByteArrayInputStream(fos.toByteArray());
      File file = TempFile.createTempFile(RandomGenerator.randomStringNumeric(10), null);
      FileUtils.copyInputStreamToFile(newInputStream, file);

      // 写入文件
      PDDocument doc = PDDocument.load(file);
      PDFRenderer renderer = new PDFRenderer(doc);
      // dpi为144,越高越清晰,转换越慢
      BufferedImage image = renderer.renderImageWithDPI(0, 144); // Windows native DPI
      // 将图片转成base64
      ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

      ImageIO.write(image, "png", byteArrayOut);
      byte[] imageBytes = byteArrayOut.toByteArray();

      BASE64Encoder encoder = new BASE64Encoder();
      String base64 = encoder.encode(imageBytes).replaceAll("\\r\\n", "").replaceAll("\\n", "");
      // 关闭文档
      doc.close();

以上代码是以PDF为模板进行数据处理、转出图片在html页面显示,这里不贴html代码了(无非是img标签,加上传的base64图片)
注意:1.创建输出流;用的ByteArrayOutputStream;
因为:读取的PDF模板输入流在数据处理时会关闭;这里用ByteArrayOutputStream 后面再转换成输入流;如果是不需要导出,直接导出处理后的pdf文件直接用response.getOutputStream()
2.处理文件填充数据时;目前在网上找到的(我个人找到的)没办法处理数据;后面找了大佬给的方法:
PdfStamper stamper = new PdfStamper(reader, fos, ‘\0’, true);
这是必要的,不然没办法处理数据(也许有其他方法)
3.处理填充数据中文不显示、乱码问题
BaseFont bfChinese = BaseFont.createFont(“/font/simsun.ttf”, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
createFont --第一个参数一般看要求是什么字体(但是要是中文字体库的字体);
第二个参数encoding—BaseFont.IDENTITY_H 填写这个我这边测试未发现有中文不显示、乱码问题(也许有其他值可替代);
第三个参数就这样吧;

已下是需要的依赖:

 <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>fontbox</artifactId>
      <version>2.0.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>2.0.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

这里是用的XY坐标定位的方法填充数据;以后遇到关键字填充方法在进行补充

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值