Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not display PDF (format invalid) on Android #105

Closed
matteoferigo opened this issue Mar 23, 2017 · 10 comments
Closed

Can not display PDF (format invalid) on Android #105

matteoferigo opened this issue Mar 23, 2017 · 10 comments

Comments

@matteoferigo
Copy link

Hi, I'm trying to open a file located in cordova.file.cacheDirectory, I open successfully images files, but when I'm opening a PDF a popup shows me:

Can not display PDF (filename.pdf is in an invalid format)

My code is like this:

$cordovaFile.checkFile(folder, fileName)
.then((fs) => {
  console.log('File system', url, fs)
  fs.file((data) => {
    const url = `${folder}${fileName}`;
    console.log('File data', url, data)
    $cordovaFileOpener2.open(url, data.type)
    .then(
      (response) => console.log('File is open', response),
      (error) => console.error('cordovaFileOpener2', error)
    )
  })
})
.catch((error) => console.error('cordovaFile', error))

The console log:

File system /data/data/APP_ID/cache/FILENAME.pdf
FileEntry
  filesystem: FileSystemfullPath: "/data/data/APP_ID/cache/FILENAME.pdf"
  isDirectory: false
  isFile: true
  name: "FILENAME.pdf"
  nativeURL: "file:///data/data/APP_ID/cache/FILENAME.pdf"
  __proto__: Entry

File data /data/data/APP_ID/cache/FILENAME.pdf
File
  end: 194
  lastModified: 1490258571000
  lastModifiedDate: 1490258571000
  localURL: "cdvfile://localhost/root/data/data/APP_ID/cache/FILENAME.pdf"
  name: "FILENAME.pdf"
  size: 194
  start: 0
  type:"application/pdf"
  __proto__: Object

File is open undefined
@matteoferigo matteoferigo changed the title Can not display PDF (format invalid) Can not display PDF (format invalid) on Android Mar 23, 2017
@pwlin
Copy link
Owner

pwlin commented Mar 23, 2017

Maybe your PDF file is corrupt?

@matteoferigo
Copy link
Author

The same file is readable on iOS. I think that the problem is with PDF on Android platform. I can open images without any problem, but seams like Android cannot read PDF files.

I am using a Samsung Galaxy S5 running Android 6.0.1

@pwlin
Copy link
Owner

pwlin commented Mar 24, 2017

Can you open your pdf with inappbrowser plugin?

@xSEx
Copy link

xSEx commented Mar 25, 2017

I have nearly the same problem.
On my desk, I have 2 Android devices. The first one with Android 4.4.2 and the other one with Android 6.0.
I installed my programmed application on both devices. They should download the files from a server folder to my device (Cordova File Transfer) and open it afterwards (File Opener 2).
The elder device with Android 4.4.2 downloads and opens the PDF without a problem.
But the problems appear on the device with Android 6.0. The PDF-files are downloaded, but not opened.
I connected the device to my computer and had a look in the console log by the ADB plugin for Chrome .
The console logs this:

Error status: undefined - Error message: undefined

Is there a solution for this problem?
Or is this a known problem?

@pwlin
Copy link
Owner

pwlin commented Mar 25, 2017

Try to open your pdf with inappbrowser plugin. It might be much easier for you.

@xSEx
Copy link

xSEx commented Mar 25, 2017

It doesn't work with the inappbrowser. There i get only blank pdfs.
I tried several files and sources (stored in the app/ stored on a server) but none of them worked.
The inappbrowser will display my downloaded jpg-file but not the pdf.

@pwlin
Copy link
Owner

pwlin commented Mar 26, 2017

inappbrowser has a much larger community around it. Can you please ask the same question over there? They might even have a slack chat group or something with people who might be able to really help. I think if you get your pdf working with inappbrowser, you can use the same file path to call fileopener2.

@matteoferigo
Copy link
Author

Problem found! The file url (placed on a NGINX server) redirects to a https url, so the download via $cordovaFileTransferSource.download corrupts the file and made it not possible to open it up.

Anyway it's not possible to read from cordova.file.applicationDirectory on Android devices, so I changed my code like this:

const tempFolder = cordova.file.externalDataDirectory;

// check if file is located in temp folder
$cordovaFile.checkFile(tempFolder, fileName)
.then(openFileEntry)
.catch(() => {
  // if file not, copy into temp folder
  $cordovaFile.copyFile(srcFolder, fileName, tempFolder, fileName)
  .then(openFileEntry)
  .catch(({message}) => reportError(message))
})

function openFileEntry(fs) {
  const filePath = fs.nativeURL;
  // get mime
  fs.file(({type}) => {
    $cordovaFileOpener2.open(filePath, type)
  });
}

@pwlin
Copy link
Owner

pwlin commented Mar 31, 2017

Amazing! Thank you for sharing your solution.

@hathemi
Copy link

hathemi commented Mar 22, 2020

i have the same problem. so how did you manage to download pdf file ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants