Android Question B4A Problem viewing PDFs

Unobtainius

Active Member
Licensed User
Longtime User
I have been creating PDFs with keirS (2014) cPDF class.
It has worked very well and I have been able to open the PDF it created with several different intents.
Its only when I try to open it locally with an Intent using DonManfred's PDFium library that I encounter errors.
Yes I'm probably doing something fundamentally wrong, but I haven't been able to figure it out.

Problem Sub:
Private Sub Button3_Click
    ' read and display the PDF that was created
    Dim PDF As PdfiumCore
    Dim folder2 As String = xui.DefaultFolder & "/temp"
    Try
        PDF.Initialize("PDFium")
        Dim cfg As Configurator = PDFView1.fromUri(folder2, "test.pdf")
        cfg.SetEventname("PDFium")
        cfg.autoSpacing(True).enableSwipe(True).pageSnap(True).swipeHorizontal(False).addOnErrorListener.addOnLoadCompleteListener.addOnPageChangeListener.addOnPageErrorListener.addOnTapListener
        cfg.load
    Catch
        MsgboxAsync(LastException, "What?")
    End Try

End Sub

I have included a test project that contains my problem.
The premise of the app is simple
Add some of your own text
Click the create button
Click the Intent Open button to open the created file using whatever intent you like for PDFs
If you entered some text you will see it in the created PDF, so its proof a new PDF is being created each time
The if you click the Open Local button you will encounter this error (in debug mode)

java.io.IOException: cannot create document: File not in PDF format or corrupted.
at com.shockwave.pdfium.PdfiumCore.nativeOpenDocument(Native Method)
at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:135)
at com.github.barteksc.pdfviewer.source.UriSource.createDocument(UriSource.java:38)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:49)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)


It may be obvious to those that know java inside out, but like I say, I cant figure out what's wrong.

What's in the project
1707127341189.png

[B4X] PDF Generator - B4X Cross Platform - Class 100% B4X Code
DonManfred's PDFium

Any suggestions are welcome.
Even if those comments are along the line of, why are you using that library?
 

Attachments

  • PDF_Test2.zip
    22.1 KB · Views: 31

Unobtainius

Active Member
Licensed User
Longtime User
my bad, its not keirS's code, although it probably was at some point, I've tried so many. Its spsp's code. Sorry @spsp
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You are creating the pdf locally. Why trying Intent to open it?
Just open it internally with PDFium

BTW You need to export B4XPages projects with the exportlink in mainpage

B4X:
private Sub savePDF(apdf As cPDF, afile As String, acompress As Int)
    Dim folder As String
    folder = Starter.fFileProvider.SharedFolder
    apdf.saveToFile(folder, afile, acompress)
    Log("File written OK? " & File.Exists(folder, afile))

    Dim PDF As PdfiumCore
    Dim folder2 As String = Starter.fFileProvider.SharedFolder ' xui.DefaultFolder & "/temp"
    'Try
        PDF.Initialize("PDFium")
        Dim cfg As Configurator = PDFView1.fromUri(folder2, "test.pdf")
        cfg.SetEventname("PDFium")
        cfg.autoSpacing(True).enableSwipe(True).pageSnap(True).swipeHorizontal(False).addOnErrorListener.addOnLoadCompleteListener.addOnPageChangeListener.addOnPageErrorListener.addOnTapListener
        cfg.load
    'Catch
    '    MsgboxAsync(LastException, "What?")
    'End Try

End Sub
 
Last edited:
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
The intent was to prove that other programs open the pdf just fine, but pdfium complains it's not a pdf or it is corrupted.
 
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User

Attachments

  • Project.zip
    26 KB · Views: 32
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
The intent works perfectly fine. That's how I know the PDF the program creates a valid PDF.
On my tablet, running in design mode, I get the error below when the cfg.load line is executed
java.io.IOException: cannot create document: File not in PDF format or corrupted.
at com.shockwave.pdfium.PdfiumCore.nativeOpenDocument(Native Method)
at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:135)
at com.github.barteksc.pdfviewer.source.UriSource.createDocument(UriSource.java:38)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:49)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)


That's why I wondered if there was something wrong with my code.
But since you can obviously run the project fine on your device, I have to assume there something deficient about my tablet and my phone.
The tablet is a Samsung S6 with Android 12 and the phone is Samsung Note10 also running Android 12, so they are quite old.

Given it seems to be a device problem I will just write to a canvas and display that to the user instead of using PDFium.
Thanks for your time, it's much appreciated.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
On my tablet, running in design mode, I get the error below when the cfg.load line is executed
add some debug-log before this line.
add path, filename, length of file, does file exists true/false to the log

I guess something is not correct here.
 
Upvote 0
Solution

Unobtainius

Active Member
Licensed User
Longtime User
Thanks for persevering with me @DonManfred
File.exists returned false
Not getting more visibility on the problem by using logging was a noobie mistake. Please accept my apologies.
I removed my code to save the file in two places and just stuck with the cPDF.fFileProvider to save the PDF to a shared folder, and then I open it from the same place. It now opens perfectly OK.
Its people like you @DonManfred that make this the best forum with the most helpful members on the internet.
 
Upvote 0
Top