某些业务场景下,需要提供相关的电子凭证,如银行回单,电子合同等。

需要生成对应的 PDF 提供给客户。

简单demo实现

添加依赖:

<dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>5.5.11</version>
  </dependency>

实现代码:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
  
/**
 * Created by xiaoming on 2019/5/20.
 */
public class JavaToPdf {
  
    private static final String DEST = "target/HelloWorld.pdf";
  
    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
        document.open();
        document.add(new Paragraph(" 520 "));
        document.close();
        writer.close();
    }
}

结果如下:

生成 helloword.pdf 文件

image.png图片

https://mp.weixin.qq.com/s/auSyOcpnDl-XAiJg1gxMbg