I am generating a pdf using pdfMake and writing it to an android device so that I can open it.
I am using Capacitor and the file system command does not appear to take a blob (this worked in cordova) so I am sending it a base64.
It seems to write ok but when I open the pdf it says the format is invalid.
Yes I write the data to the console from the variable to check it. I have used it from here to manually convert to a pdf fine. It does not have a mime/type prefix in the string but otherwise works fine.
No exception or warning. The only issue is an error from the google pdf app saying the file is invalid when it tries to open.
This is my issue. Capacitor does not take blobs. In cordova I was using a blob (worked fine) but now I have to use base64. Capacitor writeFile only takes a string for the data
Except that, if you print out to the console the value of writeFileResult, whatās the result?
Also do you get anything as result for getUriResult or just empty?
If not empty, the value of getUriResult.uri seems ok?
If yes, if you donāt use fileOpener but for example readFile to get the content of the saved file, do you get empty or do you get data?
Yeah I was thinking about manually adding a prefix to see if that worked. I will give it a try, Hopefully base64 is the format I am meant to use, there is no detailed documentation I can find. https://capacitor.ionicframework.com/docs/apis/filesystem
writeFileResult returns an empty object: ā{ }ā
getUriResult gets the file path and name and is used to successfully open the file. Value: āfile:///data/user/0/io.ionic.starter/files/defectreport.pdfā
let me know if that work with adding the the prefix, Iām curious
where you browse the files of your phone, do you find that file file:///data/user/0/io.ionic.starter/files/defectreport.pdf? I mean not using code I mean really while using the file explorer
I will try that tomorrow too. To be honest I donāt even know how to find that file path on an android phone manually. I tried and failed. My personal phone is an iphone haha.
Well putting the MIME type prefix on didnāt help.
It works fine (without prefix) if I cut and paste the string and covert to pdf file using this: http://base64converter.com/
It turns out that it is just writing it as a text file. I should have checked for that ages ago. I guess this means the capacitor file system canāt write a pdf with a base64 string.
Or maybe I need to change the encoding from UTF8 to something else?
You sir are a genius! I would buy you a beer if you were in my timezone Great idea to look at the code. I got it working.
I just needed to remove the UTF8 encoding.
Hello @wekas
Iām trying to create a file with PDF format.
Regardless of the content of the PDF being a base64 or simply a text I would have to be able to open the same one, however I can not open any PDF file
I know its not what you want but if you use this it should work for text:
Filesystem.writeFile({
path: 'doc.txt',
data: 'I am a line of text',
directory: FilesystemDirectory.Documents
encoding: FilesystemEncoding.UTF8 //Any item here assumes it is text
})
As for the pdf here is what I would try:
MIME type:
Do you have a MIME type on your Base64 data? You may need it for the pdf app on the device to recognise it.
I add it in my code:
Try writing you base64 string to the console and copy and paste it into an online pdf generator like this: http://base64converter.com/
Find the file on your device and email to computer then open it in a browser (different app). If it doesnāt work try changing the extension to .txt to make sure it is not text.
The solution for the FILE_NOTCREATED error, you should add to the <application> in AndroidManifest:
android:requestLegacyExternalStorage="true"
The solution for the error: package android.support.v4.content does not exist, try this:
//replace
public class FileProvider extends android.support.v4.content.FileProvider
//for this
public class FileProvide extends androidx.core.content.FileProvider
Man, you saved my day. Spent so much time analyzing where it was going wrong.Though this does seem unexpected behavior. There should be better documentation.