适用于 Android 的 FOP?

发布于 2024-10-18 15:24:51 字数 3427 浏览 7 评论 0原文

我最近使用 FOP 编译了一个 Android 应用程序,因为我想将一些 XML 转换为 PDF 文件。不过,我不认为 FOP JAR 文件不适用于 Android。有适用于 Android 的 FOP 版本吗?或者我可以在我的应用程序中使用任何其他 XML 到 PDF 转换器,而不是连接到互联网上启用 FOP 的服务器?

我尝试过包含 fop.jar 和 xmlgraphics.jar,但即使将它们添加到我的项目中,对 FopFactory.newInstance() 的调用也会失败。

这是调用 FOP 的按钮单击的代码片段:

// Add button listener
btnCreatePDF.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {   
                FopFactory fopFactory = FopFactory.newInstance();
                OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("/sdcard/test.pdf")));
                Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer();
                Source src = new StreamSource(new File("/sdcard/test.fo"));
                Result res = new SAXResult(fop.getDefaultHandler());

                transformer.transform(src, res);

                out.close();
            }
        });

编译器还给了我一堆 Dxwarning: Ignoring InnerClasses attribute... 错误。当我单击按钮创建 PDF 时,出现以下错误:

02-22 14:16:23.641: WARN/System.err(5590): java.lang.UnsupportedOperationException: Don't know how to handle "application/pdf" as an output format. Neither an FOEventHandler, nor a Renderer could be found for this output format.
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.render.RendererFactory.createFOEventHandler(RendererFactory.java:361)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.fo.FOTreeBuilder.(FOTreeBuilder.java:105)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.apps.Fop.createDefaultHandler(Fop.java:101)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.apps.Fop.(Fop.java:79)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:271)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:248)
02-22 14:16:23.641: WARN/System.err(5590):     at com.zebdor.signal88.ProposalCalculator$3.onClick(ProposalCalculator.java:158)
02-22 14:16:23.641: WARN/System.err(5590):     at android.view.View.performClick(View.java:2501)
02-22 14:16:23.641: WARN/System.err(5590):     at android.view.View$PerformClick.run(View.java:9107)
02-22 14:16:23.641: WARN/System.err(5590):     at android.os.Handler.handleCallback(Handler.java:587)
02-22 14:16:23.641: WARN/System.err(5590):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 14:16:23.641: WARN/System.err(5590):     at android.os.Looper.loop(Looper.java:123)
02-22 14:16:23.641: WARN/System.err(5590):     at android.app.ActivityThread.main(ActivityThread.java:3812)
02-22 14:16:23.641: WARN/System.err(5590):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 14:16:23.641: WARN/System.err(5590):     at java.lang.reflect.Method.invoke(Method.java:507)
02-22 14:16:23.641: WARN/System.err(5590):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-22 14:16:23.641: WARN/System.err(5590):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-22 14:16:23.641: WARN/System.err(5590):     at dalvik.system.NativeStart.main(Native Method)

I recently compiled an Android app using FOP as I want to convert some XML into a PDF file. However I don't think the FOP JAR files are mean't to work on Android. Are there any versions of FOP available for Android? Or any other XML to PDF converter I could use within my app instead of connecting to a FOP enabled server on the Internet?

I've tried including fop.jar and the xmlgraphics.jar but even with those added to my project, the call to FopFactory.newInstance() fails.

Here's my code snippet for the button click that calls FOP:

// Add button listener
btnCreatePDF.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {   
                FopFactory fopFactory = FopFactory.newInstance();
                OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("/sdcard/test.pdf")));
                Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer();
                Source src = new StreamSource(new File("/sdcard/test.fo"));
                Result res = new SAXResult(fop.getDefaultHandler());

                transformer.transform(src, res);

                out.close();
            }
        });

The compiler is also giving a me a bunch of Dxwarning: Ignoring InnerClasses attribute... errors. Here's the error when I click the button to create the PDF:

02-22 14:16:23.641: WARN/System.err(5590): java.lang.UnsupportedOperationException: Don't know how to handle "application/pdf" as an output format. Neither an FOEventHandler, nor a Renderer could be found for this output format.
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.render.RendererFactory.createFOEventHandler(RendererFactory.java:361)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.fo.FOTreeBuilder.(FOTreeBuilder.java:105)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.apps.Fop.createDefaultHandler(Fop.java:101)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.apps.Fop.(Fop.java:79)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:271)
02-22 14:16:23.641: WARN/System.err(5590):     at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:248)
02-22 14:16:23.641: WARN/System.err(5590):     at com.zebdor.signal88.ProposalCalculator$3.onClick(ProposalCalculator.java:158)
02-22 14:16:23.641: WARN/System.err(5590):     at android.view.View.performClick(View.java:2501)
02-22 14:16:23.641: WARN/System.err(5590):     at android.view.View$PerformClick.run(View.java:9107)
02-22 14:16:23.641: WARN/System.err(5590):     at android.os.Handler.handleCallback(Handler.java:587)
02-22 14:16:23.641: WARN/System.err(5590):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 14:16:23.641: WARN/System.err(5590):     at android.os.Looper.loop(Looper.java:123)
02-22 14:16:23.641: WARN/System.err(5590):     at android.app.ActivityThread.main(ActivityThread.java:3812)
02-22 14:16:23.641: WARN/System.err(5590):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 14:16:23.641: WARN/System.err(5590):     at java.lang.reflect.Method.invoke(Method.java:507)
02-22 14:16:23.641: WARN/System.err(5590):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-22 14:16:23.641: WARN/System.err(5590):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-22 14:16:23.641: WARN/System.err(5590):     at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-10-25 15:24:51

对于 Android,您不能只从 Java 库中获取 .jar 并在 Android 应用程序中使用它。 Android 使用 Dalvik JVM (http://en.wikipedia.org/wiki/Dalvik_(software)),它与 ​​Java SE API 的完整 API 不 100% 兼容,而是仅支持 API 的子集。因此,您可能使用的许多 Java 库(例如 FOP)与 Android 不兼容,需要专门化才能在 Android 设备上正常运行。祝你好运。

With Android, you can't just take a .jar from a Java library and use it in an Android application. Android uses the Dalvik JVM (http://en.wikipedia.org/wiki/Dalvik_(software)) which is not 100% compatible with the full API from the Java SE API, and instead only supports a subset of the API. For this reason, many of the Java libraries you might be use to, like FOP, are not Android compatible as-is and require specialization before such libraries will function properly on an Android device. Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文