我正在尝试开发一个将 pdf 文档转换为 jpeg 图像的网络服务(基于 jersey)。我选择 GhostScript 是因为我对它和它的结果有很好的经验(尤其是嵌入字体)。所以我搜索了一种在 Java 中使用 GhostScript 的方法,并找到了 Ghost4j。
因此,我将所有 Ghost4j jar 放入我的应用程序 lib 文件夹(也是 jna.jar)。在我的第一次测试中,我遇到了多次执行任务的问题,因为 jna 在多次启动时会引发错误。所以我把 jna.jar 放到了 tomcat lib 文件夹中。这工作得更好一点,但它一次只执行一项任务。如果我同时开始另一个,什么都没有发生。我只是似乎没有执行其他任务。
所以我试图setMaxProcessCount(2);
让我的应用程序一次执行多个任务。这是我的代码:
private static void generateImages(String inputFile, String outputPath) throws IOException, RendererException, org.ghost4j.document.DocumentException {
PDFDocument document = new PDFDocument();
document.load(new File(inputFile));
SimpleRenderer renderer = new SimpleRenderer();
renderer.setMaxProcessCount(2);
renderer.setResolution(150);
renderer.setAntialiasing(4);
System.setProperty("jna.library.path", "C:\\Program Files\\gs\\gs9.09\\bin\\");
List<Image> images = renderer.render(document);
for (int i = 0; i < images.size(); i++) {
ImageIO.write((RenderedImage) images.get(i), "jpeg", new File(outputPath + File.separatorChar + (i + 1) + ".jpeg"));
}
}
但是如果我试图执行我的任务,Ghost4j 会抛出这个错误:
org.ghost4j.renderer.RendererException: java.lang.Exception: java.lang.NoClassDefFoundError: com/sun/jna/Structure
at org.ghost4j.renderer.AbstractRemoteRenderer.render(AbstractRemoteRenderer.java:133)
at PdfResource.ConversionTask.generateImages(ConversionTask.java:218)
at PdfResource.ConversionTask.exec(ConversionTask.java:58)
at PdfResource.Task.run(Task.java:86)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.Exception: java.lang.NoClassDefFoundError: com/sun/jna/Structure
at gnu.cajo.invoke.Remote.invoke(Remote.java:594)
at gnu.cajo.invoke.Remote.invoke(Remote.java:722)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy165.invoke(Unknown Source)
at gnu.cajo.invoke.Remote.invoke(Remote.java:565)
at org.ghost4j.renderer.AbstractRemoteRenderer.render(AbstractRemoteRenderer.java:126)
... 4 more
Caused by: java.lang.NoClassDefFoundError: com/sun/jna/Structure
at org.ghost4j.renderer.SimpleRenderer.run(SimpleRenderer.java:68)
at org.ghost4j.renderer.AbstractRemoteRenderer.remoteRender(AbstractRemoteRenderer.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at gnu.cajo.invoke.Remote.invoke(Remote.java:582)
at gnu.cajo.invoke.Remote.invoke(Remote.java:722)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
... 1 more
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Structure
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 23 more
我猜这与 Ghost4j 正在启动的附加 JVM 有关系,而且它似乎没有使用与 tomcat 相同的类路径,但我并不是真正的 Java 专家,也不知道如何解决这个问题。