0

I have requirement to write pdf to phone storage using Phonegap, but phonegap only supports text writting in file. I have base64 string which contains pdf doc, and I want to save it to Phonestorage. I have tried to convert base64 to utf-8 using atob function and then write it to pdf, but it is not also working.

Is there something else that I should try? or is there any plugin available?

Any help would be appreciated.

Mohammad Ashfaq
  • 1,333
  • 2
  • 14
  • 38

2 Answers2

0

Create a normal pdf writer method in java. For the same iText or any other pdf writers can be used. Expose a public method in this writer class as a Javascript Interface. Eg :

Class PdfWriter{

@JavascriptInterface
    public void writeToPdf(String dataToBeWritten) {
        writeToFile(dataToBeWritten);
    }
}

Register this as a JavascriptInterface with the webview.

Once the webview is loaded, you should be able to call:

window.PdfWriter.writeToPdf("data_to_be_written")
MIdhun Krishna
  • 1,739
  • 1
  • 13
  • 31
  • I prefer to write it using Java script only, because I am creating hybrid application – Mohammad Ashfaq Jun 20 '14 at 11:28
  • Basically you are creating a Cordova Plugin manually with the above approach. From your question it seems you are fine with using a Plugin. You can also try the approach mentioned in [this](http://www.tricedesigns.com/2014/01/08/generating-pdf-inside-of-phonegap-apps/) post – MIdhun Krishna Jun 20 '14 at 11:46
0

The base64 PDF data needs to be converted to a fileblob.

You can then save that fileblob as a PDF file using the cordova-file plugin.

Convert base64 string as pdf in phonegap

Community
  • 1
  • 1
Jorn Theunissen
  • 191
  • 2
  • 14