good way to generate PDF offline on Android device

2020-04-21 03:01发布

is there a good way to generate a PDF document on a Android device? I have a DOCX file as a Base.

I know that it is possible: here a very good app https://play.google.com/store/apps/details?id=cn.wps.moffice_eng

they render it offline.

some ideas?

I already tried DOCX4J, and it is generating a DOCX file properly, how ever using the

WordprocessingMLPackage wordMLPackage to prepare the PDF it says:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
            at org.plutext.DocxToHtml.AndroidDocxToHtmlActivity$1.onClick(AndroidDocxToHtmlActivity.java:215)
            at android.view.View.performClick(View.java:4209)
            at android.view.View$PerformClick.run(View.java:17431)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5297)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at **org.docx4j.utils.ResourceUtils.getResource(ResourceUtils.java:45)
            at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.<clinit>(Conversion.java:101)**
            at org.plutext.DocxToHtml.AndroidDocxToHtmlActivity$1.onClick(AndroidDocxToHtmlActivity.java:215)
            at android.view.View.performClick(View.java:4209)
            at android.view.View$PerformClick.run(View.java:17431)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5297)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

This is the code I used:

 // 2) Prepare Pdf settings
                        PdfSettings pdfSettings = new PdfSettings();
                        pdfSettings.setImageHandler(new ConversionImageHandler() {
                            @Override
                            public String handleImage(AbstractWordXmlPicture abstractWordXmlPicture, Relationship relationship, BinaryPart binaryPart) throws Docx4JException {
                                return null;
                            }
                        });

                        // 3) Convert WordprocessingMLPackage to Pdf
                        OutputStream out = new FileOutputStream(pdffile);
                        PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
                                wordMLPackage);
                        converter.output(out, pdfSettings);
                        Log.e("AndroidDocxToHtml", " PDF save done");

EDIT UPDATE:

It turned out that the docx2fo.xslt wasn't in the library at org\docx4j\convert\out\pdf\viaXSLFO\ inserting this file into the JAR I am getting now:

java.lang.NoClassDefFoundError: org.apache.fop.apps.FopFactory
        at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:231)

which is this line:

FopFactory fopFactory = FopFactory.newInstance();

and this import:

import org.apache.fop.apps.FopFactory;

which seems to be a serious problem on Android, see here

EDIT UPDATE 2

just to be complete: adding the fop.jar containing the FopFactory it gives the following error: java.lang.NoSuchMethodError: org.apache.fop.apps.FopFactory.newInstance

Any Suggestions are welcome.

0条回答
登录 后发表回答