解决iText生成pdf文件过大的问题

为iText生成的pdf文件瘦身。html

原来生成pdf文件,即便是纯文本内容,不管内容再如何少,文件体积老是在7M多,致使传输速度很慢。经排查,是在pdf生成的时候,选用了第三方中文字体形成的。java

原生成PDF代码中,有以下语句:windows

bfChinese = BaseFont.createFont("c://windows//fonts//SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

这种状况下,iText会将字体TTF文件一同打包进pdf文件中,形成文件很大。解决方法是讲这句改写为:maven

BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

同时,须要引入itext-asian,使用maven:字体

<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

这样,原来7M多的文件,如今只有100K左右了。code