PDF、barcode4j 用 java 压缩
我正在使用 Apache Fop 1.0 创建 PDF 文档。默认情况下,它使用 FlateFilter 来压缩内容。 FOP 在幕后使用 DeflaterOutputStream DEFAULT_COMPRESSION。我还没有发现任何将其设置为 BEST_COMPRESSION 的可能性。
该 pdf 包含一些由 Barcode4j 2.1 生成的条形码。正如我所看到的,生成的图像非常巨大,而且我还没有找到任何如何压缩它的信息:
<fo:instream-foreign-object height="25pt" width="101pt">
<barcode:barcode xmlns:barcode="http://barcode4j.krysalis.org/ns" message="xxxxxxxx">
<barcode:code128>
<barcode:height>37pt</barcode:height>
<barcode:module-width>0.9pt</barcode:module-width>
<barcode:human-readable>
<barcode:placement>none</barcode:placement>
</barcode:human-readable>
</barcode:code128>
</barcode:barcode>
</fo:instream-foreign-object>
如果我生成 PDF(在 fop 中使用 flate 并使用上面编写的条形码注入),那么我可以使用简单的 winzip 来减小 pdf 大小30%。
有什么办法可以通过我的过程使用更好的压缩算法吗? pdf 应该可以在 Acrobat Reader 中打开,因此不能选择在服务器端压缩并在客户端解压缩。
我认为barcode4j生成的图像可以被压缩,但我不知道该怎么做。
更新1: 生成的条形码似乎太大。该 pdf 包含 3 页和 3 个条形码。 3 个条形码大约占整个 pdf 大小的 50%。
谢谢佐尔坦
I am using Apache Fop 1.0 to create PDF documents. By default it uses the FlateFilter to compress the content. The FOP uses the DeflaterOutputStream DEFAULT_COMPRESSION under the cover. I have not found any possibility to set it to BEST_COMPRESSION.
The pdf contains some barcodes as well generated by Barcode4j 2.1. As I see the generated image is quite huge and I have not found any info how compress it:
<fo:instream-foreign-object height="25pt" width="101pt">
<barcode:barcode xmlns:barcode="http://barcode4j.krysalis.org/ns" message="xxxxxxxx">
<barcode:code128>
<barcode:height>37pt</barcode:height>
<barcode:module-width>0.9pt</barcode:module-width>
<barcode:human-readable>
<barcode:placement>none</barcode:placement>
</barcode:human-readable>
</barcode:code128>
</barcode:barcode>
</fo:instream-foreign-object>
If I generate the PDFs (using flate in fop and using the barcode injections written above), then I could decrease the pdf size with simple winzip by 30 percent.
Is there any way to use a better compression algorithm by my process?
The pdf should be openable in Acrobat Reader, so it is not an option to zip at server side and unzip it on the client side.
I think the barcode4j generated image could be compressed, but I can not see how to do it.
Update1:
The generated barcode seems to be too big. The pdf contains 3 pages and 3 barcodes. The 3 barcode is about the 50% of the whole pdf size.
Thanks
Zoltan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认压缩设置和最大压缩设置没有太大区别。如果你了解 ZLib,你可以写一些测试代码来看看——可能比你预期的要少得多。大小缩小 30% 的原因是压缩应用于整个文件 - 在 PDF 中仅压缩流。
您可以通过使用压缩对象流来显着减小大小。 FOP 不支持它,但许多工具支持。如果您需要更多信息,请参阅此处 。
There is no big difference between the default compression setting and the max. If you know ZLib, you can write some test code to see - probably much less than you expected. The reason that you get 30% smaller in size is that compression is applied on the whole file - in PDF only streams are compressed.
You can substantially reduce the size by using compressed object streams. FOP does not support it but many tools do. See here if you need more information.