0

Ho to everyone. I followed this tutorial to create a modal view with a pdf generated with pdfmake.

http://gonehybrid.com/how-to-create-and-display-a-pdf-file-in-your-ionic-app/

My simply question is how can i save the pdf in my local storage on in cache? I need that to send the pdf by email or open it with openfile2. I'm using Ionic and cordova.

Steph8
  • 1,483
  • 2
  • 16
  • 32
  • you can use either use file transfer plugin or your custom code to save the file in device local storage. Check the sample code under the file creation section in the following post - http://stackoverflow.com/questions/36037359/unable-to-delete-file-using-cordova/36038741#36038741 – Gandhi Mar 21 '16 at 12:21
  • thanks for the answer. I installed that plugin, but i didn't know in that example what i need to use to store the file. The base64 code? The blob? I'm new and idk that. – Steph8 Mar 21 '16 at 13:37
  • please find the sample code in my answer section. Hope it helps. – Gandhi Mar 21 '16 at 14:37

2 Answers2

0

I don't know how you code it, but I know what plugin you should use:

https://github.com/apache/cordova-plugin-file

The git contains a complete documentation of the plugin so everything you could need should be there.

Jur Clerkx
  • 698
  • 4
  • 14
  • thanks for the answer. I installed that plugin, but i didn't know in that example what i need to use to store the file. The base64 code? The blob? I'm new and idk that. – Steph8 Mar 21 '16 at 13:37
  • You should be able to find more information over [here](http://docs.phonegap.com/en/edge/cordova_file_file.md.html#FileWriter) – Jur Clerkx Mar 21 '16 at 14:15
  • i read it, but i didn't understand what i need to write a pdf. Now i know how to write a file with the $cordovaFile.writeFile(cordova.file.dataDirectory, "file.txt", "text", true), where the first is the directory (i test it and i know i need to write on externalstorage to read the file after), new second is the name of the file and the third is the content. This last item i miss. What i need to use to write a pdf? The base64? idk this. – Steph8 Mar 21 '16 at 14:22
0

Sample code to write pdf file in device using cordova file and file transfer plugin:

var fileTransfer = new FileTransfer();

    if (sessionStorage.platform.toLowerCase() == "android") {
        window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
    } else {
        // for iOS
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
    }

    function onError(e) {
        navigator.notification.alert("Error : Downloading Failed");
    };

    function onFileSystemSuccess(fileSystem) {
        var entry = "";
        if (sessionStorage.platform.toLowerCase() == "android") {
            entry = fileSystem;
        } else {
            entry = fileSystem.root;
        }
        entry.getDirectory("Cordova", {
            create: true,
            exclusive: false
        }, onGetDirectorySuccess, onGetDirectoryFail);
    };

    function onGetDirectorySuccess(dir) {
        cdr = dir;
        dir.getFile(filename, {
            create: true,
            exclusive: false
        }, gotFileEntry, errorHandler);
    };

    function gotFileEntry(fileEntry) {
        // URL in which the pdf is available
        var documentUrl = "http://localhost:8080/testapp/test.pdf";
        var uri = encodeURI(documentUrl);
        fileTransfer.download(uri, cdr.nativeURL + "test.pdf",
            function(entry) {
                // Logic to open file using file opener plugin
            },
            function(error) {
                navigator.notification.alert(ajaxErrorMsg);
            },
            false
        );
    };
Gandhi
  • 11,875
  • 4
  • 39
  • 63
  • that's perfect, but my problem is i know how to write a pdf, but i can't understand how to write the pdf in that example. I can't found where the pdf file are stored in that example, or where the data are stored.. – Steph8 Mar 21 '16 at 14:46
  • @Steph8, sorry i dint know that you are following an example and trying it out.I dont have much idea about ionic framework. But my wild guess is try alerting '$scope.pdfUrl' to see if its returning you any local device URL where the file is residing. Jus a guess though. – Gandhi Mar 21 '16 at 15:00
  • unfortunally $scope.pdfUrl is blob:http%3A//172.31.5.9%3A8100/3c9dbb3d-ff85-4c5a-9d21-97f85e516226 not a local url :( – Steph8 Mar 21 '16 at 15:06
  • in the function onGetDirectorySuccess(), what is the variable "filename"? Can this be any name or is it something that was defined before? – Jorrex Jun 23 '16 at 19:14
  • @Jorrex it can be any name with proper file extension. Check this out for more clarity - https://github.com/gandhirajan/Cordova_File_Operations please do upvote if it was helpful so that it will beneficial for others too – Gandhi Jun 23 '16 at 20:16
  • iOS version works on Android now. Tested with Cordova 9 and Android 6. – v.nivuahc Aug 05 '20 at 16:21