Invalid Format of "filename.pdf" when we open it on success of WriteFile Api


#1

On success of this WriteFile API it generates a .pdf file.
but when I open it through fileOpener or I open it manually while going it into that directory it gives me error that test.pdf is of Invalid Format

When I generate .docx file or .txt file it is opening and displaying the data.

this.file.writeFile(cordova.file.externalDataDirectory,"test.pdf","HEllO WORLD",true).then(succ=>{
      console.log("File write success : ", succ)
      this.fileOpener.open(
      succ.nativeURL,
      'application/pdf'
    ).then((success) => {
      console.log('success open file: ', success);
    }, (err) => {
      console.log('error open file', err);
    });

    },
    err=>{
      console.log(" write File error : ", err)
    })

I am facing this issue.


#2

What is this? Provide a link to the library (and Ionic Native plugin if this is one).


#3

I have imported File from ionic-native

import { File } from '@ionic-native/file';

Constructor defined for the File API

Constructor(){
 private file: File
}

So in this.file.writeFile i am accessing the File api.

link https://ionicframework.com/docs/native/file/

@Sujan12


#4

Okay, then it is quite clear: You can just write plain text to a PDF file, PDF is a binary format.
Try to open a working PDF file in Notepad (or your favorite editor) to see what this means.


#5

Other files are opening of .pdf format.

Only the .pdf file which I am creating and opening from Ionic File Api is not opening.

It gives error.

@Sujan12


#6

Yes, see what I wrote again: You can’t just create a PDF using writeFile. You can only create plain text files with that or if you replace the “HEllO WORLD” with the required binary format of PDF.