TomRoush / PdfBox-Android

The Apache PdfBox project ported to work on Android
Apache License 2.0
1.01k stars 259 forks source link

Cannot init PDFTextStripper, code cannot find "glyphlist" file. There is no "Resources" folder in com/tom_roush/pdfbox/ #577

Open olamao opened 3 weeks ago

olamao commented 3 weeks ago

Describe the bug I was trying to create first instance of PDFTextStripper and it crashed. I am trying to extract text from pdf. The log says that it is looking for file "glyphlist" in "com/tom_roush/pdfbox/resources" but I couldn't find that folder in the "External libraries" of android studio (Android Studio Koala | 2024.1.1 Patch 1). I am using a MAC OS ventura 13.6.9. .

To reproduce Code snippet to reproduce the behavior:

URI uri = new URI(inputString); Path uriPath = null; boolean exists = false; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { uriPath = Path.of(uri); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { exists = Files.exists(uriPath); } }

        System.out.println("exist? cannot believe it " + exists);
        System.out.println("path" + uriPath);
        File pdfFile = new File(String.valueOf(uriPath));
        PDDocument document = PDDocument.load(pdfFile);
        PDFTextStripper myStripper = new PDFTextStripper();
        String myResult = myStripper.getText(document);
System.out.println("Hello world");

FATAL EXCEPTION: main
                                                                                                    Process: com.example.app, PID: 28519
                                                                                                    java.lang.ExceptionInInitializerError
                                                                                                        at com.tom_roush.pdfbox.text.LegacyPDFStreamEngine.<clinit>(LegacyPDFStreamEngine.java:102)
                                                                                                        at com.example.app.FirstJavaClass.myFun(FirstJavaClass.java:38)
                                                                                                        at com.example.app.MainActivityKt.BodyContent(MainActivity.kt:83)
                                                                                                        at com.example.app.MainActivityKt$BodyContent$2.invoke(Unknown Source:12)
                                                                                                        at com.example.app.MainActivityKt$BodyContent$2.invoke(Unknown Source:10)
                                                                                                        at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:192)
                                                                                                        at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2556)
                                                                                                        at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2827)
                                                                                                        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3314)
                                                                                                        at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:3265)
                                                                                                        at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:940)
                                                                                                        at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:1155)
                                                                                                        at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:127)
                                                                                                        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:583)
                                                                                                        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:551)
                                                                                                        at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:41)
                                                                                                        at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
                                                                                                        at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
                                                                                                        at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
                                                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1632)
                                                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1643)
                                                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:1172)
                                                                                                        at android.view.Choreographer.doFrame(Choreographer.java:1072)
                                                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1607)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:958)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:224)
                                                                                                        at android.os.Looper.loop(Looper.java:318)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8727)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
                                                                                                        Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.runtime.PausableMonotonicFrameClock@7aefc8, androidx.compose.ui.platform.MotionDurationScaleImpl@6173861, StandaloneCoroutine{Cancelling}@716bf86, AndroidUiDispatcher@aae9047]
                                                                                                    Caused by: java.lang.RuntimeException: java.io.IOException: GlyphList 'com/tom_roush/pdfbox/resources/glyphlist/glyphlist.txt' not found
                                                                                                        at com.tom_roush.pdfbox.pdmodel.font.encoding.GlyphList.load(GlyphList.java:69)
                                                                                                        at com.tom_roush.pdfbox.pdmodel.font.encoding.GlyphList.<clinit>(GlyphList.java:39)
                                                                                                        ... 32 more
                                                                                                    Caused by: java.io.IOException: GlyphList 'com/tom_roush/pdfbox/resources/glyphlist/glyphlist.txt' not found
                                                                                                        at com.tom_roush.pdfbox.pdmodel.font.encoding.GlyphList.load(GlyphList.java:63)
                                                                                                        ... 33 more

PDF example Link or attach a PDF demonstrating the issue text.pdf

Expected behavior A clear and concise description of what you expected to happen.

I am trying to extract text from a pdf file. I created a new PDFTextStripper. So I expect to be able to create a string with the text.

Actual behavior A clear and concise description of what happened instead.

The app crashed.

Environment details:

compileSdk = 34

defaultConfig {
    applicationId = "com.example.app"
    minSdk = 24
    targetSdk = 34

Additional context Add any other context about the problem here. The log is clear, it could not find the file glyphlist, in the "resources" folder.

Thanks very much.

Mauricio Olaya

olamao commented 3 weeks ago

Solved, by adding PDFBoxResourceLoader.init(getApplicationContext()) at create main activity. I found solution in a previous issue answer. Thanks