gpt4 book ai didi

java - 为什么 PDFBox PDFRenderer 很慢?

转载 作者:行者123 更新时间:2023-12-05 01:23:32 37 4
gpt4 key购买 nike

我想使用 PDFBox 2.x 和 PDFRenderer 类将 PDF 转换为 TIFF。

但与ghostscript相比,它运行得非常慢。

这是我的示例代码

public class SpeedTest
{
static long startTime = System.currentTimeMillis ();

public static void logTime (String msg)
{
long now = System.currentTimeMillis ();
System.out.println (String.format ("%.3f: %s", (now - startTime) / 1000.0, msg));
startTime = now;
}

public static void main (String[] args) throws Exception
{
//System.setProperty ("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");

String pdfFileName = args[0];
String tiffFileName = args[1];

PDDocument document = PDDocument.load (new File (pdfFileName));
logTime (pdfFileName + " loaded.");
PDFRenderer pdfRenderer = new PDFRenderer (document);
logTime ("intitalized renderer.");
BufferedImage img = pdfRenderer.renderImageWithDPI (0, 600, ImageType.RGB);
logTime ("page rendered as image.");
ImageIO.write (img, "TIFF", new File (tiffFileName));
logTime ("image saved as TIFF.");
}
}

输出如下
0.521: sample.pdf loaded.
0.013: intitalized renderer.
2.910: page rendered as image.
2.005: image saved as TIFF.

如您所见,对 pdfRenderer.renderImageWithDPI 的调用几乎需要 3 秒( ImageIO.write -call 也需要 2 秒)。

当使用 ghostscript 完成相同的任务时,完整的任务会在 0.4 秒内完成。
time gs -dQUIET -dBATCH -dNOPAUSE -sstdout=/dev/null -sDEVICE=tifflzw -r600 -dFirstPage=1 -dLastPage=1 -sOutputFile=sample.tif sample.pdf

real 0m0.389s
user 0m0.340s
sys 0m0.048s

我也已经试过了
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");

因为我正在运行 Java 8(准确地说是 1.8.0_161),但这没有什么区别。

谢谢你的每一个想法,
问候

托马斯

最佳答案

升级 JDK 1.8.0_191 发布于 2018 年 10 月 ,或 JDK 9.0.4。
来自 Pdfbox docs ,

PDFBox and Java 8

Important notice when using PDFBox with Java 8before 1.8.0_191 or Java 9 before 9.0.4

Due to the change of the java color management module towards“LittleCMS”, users can experience slow performance in coloroperations. A solution is to disable LittleCMS in favor of the oldKCMS (Kodak Color Management System) by:

Starting with -Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvideror Calling

System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider")
资料来源:

https://bugs.openjdk.java.net/browse/JDK-8041125

关于java - 为什么 PDFBox PDFRenderer 很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49100210/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com