Message: Is a directory error when Saving Remote URL PDF to cache location - Android

Product: PDFTron SDK

Product Version: 9.0177774

Hello,

I am loading a PDF from a remote URL and annotating on them. Once the annotations are done, I want to save this to cache or external file location. I have used doc.save() method. But I am getting below error.

libc++abi: terminating with unexpected exception of type trn::Common::Exception: Exception: 
Message: Is a directory
Conditional expression: m_stream != NULL
Version      : 9.0.1.77774
Platform     : Android
Architecture : Arm
Filename     : StdFile.cpp
Function     : InitL
Linenumber   : 221

Below is my code to load and save pdf.

// Loading PDF from http url
val url = "https://example.com/myfile.pdf"
pdfviewctrl.openUrlAsync(url, cacheDir?.absolutePath, null, httpOptions)

// After adding the annotations, I am executing below to save the file to cache location. (Or I want to get the file path of annotated pdf)
val uri = FileUtils.createPdfTempFile(this@PdfAnnotatorActivity)
// the uri value is - content://com.myapp.fileprovider/external_files/Documents/Checked_20210827.pdf

// after getting the url, I am calling save() method.
// Method 1 tried:
var filter = SecondaryFileFilter(this@PdfAnnotatorActivity, uriPdf)
pdfviewctrl.doc.lock()
pdfviewctrl.doc.save(filter, SDFDoc.SaveMode.LINEARIZED)

// Method 2 tried:
val path = FileUtils.createPdfFile(this@PdfAnnotatorActivity)
// path is absolute path of pdf file
pdfviewctrl.doc.lock()
pdfviewctrl.doc.save(path, SDFDoc.SaveMode.LINEARIZED, null)

// Method to get temporaty file path
@Throws(IOException::class)
    fun createPdfFile(context: Context): String? {
        // Create an image file name
        var path: String? = null
        val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date())
        val storageDir: File? = context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)
        File.createTempFile(
            "PDF_${timeStamp}_", /* prefix */
            ".pdf", /* suffix */
            storageDir /* directory */
        ).apply {
            // Save a file: path for use with ACTION_VIEW intents
            path = absolutePath
        }
        return path
    }

I have used same method createPdfFile() to save a Generated pdf on the fly, it’s working fine. The above error is thrown when loading https url.

Update:
Now I see the above same error while adding Text Annotations also randomly.

Please help me solving this issue.

Thank You

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi, may I ask why you are using PDFViewCtrl instead of the fragment? Because this is a situation that is handled by the fragment already. What you want to do is on download finished event, re-open the document by the URL cache file, then you can operate it as a local file. Full working code can be found in the download package, in “PdfViewCtrlTabBaseFragment.java” file search “onDownloadEvent”.

Could you give that a try?

Hi Shirley_Gong

Due to our app requirement, we have to use PDFViewCtrl only. Now I could able to save the annotated PDF as you suggested. Earlier, I haven’t provided the cache file path correclty. After providing path correctly, I could able to save the file to same location.

Thank you

Hi Ravi,

That’s good to hear. The cache file is recommended as it also improves performance. Glad it works for you now.

Thanks.