afreakyelf / Pdf-Viewer

A Lightweight PDF Viewer Android library which only occupies around 80kb while most of the Pdf viewer occupies up to 16MB space.
https://afreakyelf.github.io/Pdf-Viewer/
MIT License
821 stars 173 forks source link

[BUG] IOException: file not in PDF format or corrupted #114

Closed dookki closed 8 months ago

dookki commented 8 months ago

📝 Describe the Bug

Recently I encountered some problems with the app crash IOException, about malformed links or broken links (404)..., this case does not fall into the onError function in PdfRendererView.StatusCallBack, even though I catch the exception with try, catch but it still happens

📖 Library Version


image

github-actions[bot] commented 8 months ago

Thank you for creating your first issue. We appreciate your help in making this project better. We will look into it, and get back to you soon. Need help or want to discuss this issue? Join our Discord community here to ask questions and discuss this issue live!

afreakyelf commented 8 months ago

Hey @dookki, do you mind sharing the URL of the pdf and your implementation if possible.

houkhan commented 8 months ago

Hey The url I'm using is https://css4.pub/2015/textbook/somatosensory.pdf I have also encountered related problems. When opening a relatively large pdf, when the PDF is still loading, that is, the loading animation has not disappeared, closing the page at this time will have this problem when rewriting and opening. When following the crash log.

************* Crash Head ****************
Time Of Crash      : 2024_03_26-10_14_45
Rom Info           : RomInfo{name=xiaomi, version=V12.5.1.0.QEECNXM}
Device Manufacturer: Xiaomi
Device Model       : MIX 3
Android Version    : 10
Android SDK        : 29
App VersionName    : 1.1
App VersionCode    : 2
************* Crash Head ****************

java.io.IOException: file not in PDF format or corrupted
    at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)
    at android.graphics.pdf.PdfRenderer.<init>(PdfRenderer.java:169)
    at com.rajat.pdfviewer.PdfRendererCore.openPdfFile(PdfRendererCore.kt:142)
    at com.rajat.pdfviewer.PdfRendererCore.<init>(PdfRendererCore.kt:76)
    at com.rajat.pdfviewer.PdfRendererView.init(PdfRendererView.kt:148)
    at com.rajat.pdfviewer.PdfRendererView.init(PdfRendererView.kt:143)
    at com.rajat.pdfviewer.PdfRendererView.initWithFile(PdfRendererView.kt:107)
    at com.rajat.pdfviewer.PdfRendererView$initWithUrl$1.onDownloadSuccess(PdfRendererView.kt:96)
    at com.rajat.pdfviewer.PdfDownloader.checkAndDownload(PdfDownloader.kt:54)
    at com.rajat.pdfviewer.PdfDownloader.access$checkAndDownload(PdfDownloader.kt:13)
    at com.rajat.pdfviewer.PdfDownloader$1.invokeSuspend(PdfDownloader.kt:31)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:367)
    at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
    at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
    at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
    at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
    at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
    at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
    at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
    at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
    at com.rajat.pdfviewer.PdfDownloader.<init>(PdfDownloader.kt:31)
    at com.rajat.pdfviewer.PdfRendererView.initWithUrl(PdfRendererView.kt:84)
    at com.rajat.pdfviewer.PdfViewerActivity.initPdfViewer(PdfViewerActivity.kt:360)
    at com.rajat.pdfviewer.PdfViewerActivity.loadFileFromNetwork(PdfViewerActivity.kt:351)
    at com.rajat.pdfviewer.PdfViewerActivity.init(PdfViewerActivity.kt:254)
    at com.rajat.pdfviewer.PdfViewerActivity.onCreate(PdfViewerActivity.kt:243)
    at android.app.Activity.performCreate(Activity.java:7893)
    at android.app.Activity.performCreate(Activity.java:7880)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7562)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
houkhan commented 8 months ago

If it cannot be reproduced, you can replace a relatively large pdf, then close it when it is loaded again, and then reopen it. It should be the problem that the resource is not released when the loading is not successful.

houkhan commented 8 months ago

After checking, it is found that it is caused by the if (cachedFile.exists()) of the PdfDownloader checkAndDownload. If it is closed without downloading successfully, the file is damaged and tries to modify it. However, this will trigger a re-download every time.

        if (cachedFile.exists()) {
            cachedFile.deleteRecursively()
        }
        download(downloadUrl, cachedFileName)
houkhan commented 8 months ago

Or modify it like this. This will try to download again after the pdf fails to open.

        if (cachedFile.exists()) {
            try {
                listener.onDownloadSuccess(cachedFile.absolutePath)
            } catch (e: Exception) {
                download(downloadUrl, cachedFileName)
            }

        } else {
            download(downloadUrl, cachedFileName)
        }
afreakyelf commented 8 months ago

Hi, The Above issue has been fixed in new release. https://github.com/afreakyelf/Pdf-Viewer/releases/tag/v2.1.0

Please migrate to Maven central. More here: https://github.com/afreakyelf/Pdf-Viewer/releases/tag/v2.1.0

Thank you for using the library. Please reopen/raise a new issue if you still face this error.

Join our discord for more updates!

dookki commented 8 months ago

I apologize for the delayed response. Thank you for fixing this error.

susi021 commented 7 months ago

I'm still facing the same issue when I try to open a random txt file as a PDF using PdfRendererViewCompose