Search code examples
cordovacordova-pluginsjspdf

jsPDF and cordova Cant view the pdf file


I'm trying to generate a PDF invoice for my app , I can get to the line 'console.log ("write success");' but I just cannot locate the file. Please help!!! How can I save it to a folder in my mobile???

console.log("generating pdf...");
var doc = new jsPDF();

doc.text(20, 20, 'HELLO!');

doc.setFont("courier");
doc.setFontType("normal");
doc.text(20, 30, 'This is a PDF document generated using JSPDF.');
doc.text(20, 50, 'YES, Inside of PhoneGap!');

var pdfOutput = doc.output();
console.log( pdfOutput );

//NEXT SAVE IT TO THE DEVICE'S LOCAL FILE SYSTEM
console.log("file system...");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

   console.log(fileSystem.name);
   console.log(fileSystem.root.name);
   console.log(fileSystem.root.fullPath);



fileSystem.root.getFile("test.pdf", {create: true}, function(entry) {
      var fileEntry = entry;
      console.log(entry);

      entry.createWriter(function(writer) {
         writer.onwrite = function(evt) {
         console.log("write success");
      };

      console.log("writing to file");
         writer.write( pdfOutput );
      }, function(error) {
         console.log(error);
      });

   }, function(error){
      console.log(error);
   });
},
function(event){
 console.log( evt.target.error.code );

Solution

  • You are saving your files in the root path of the system.

    If you want to select the folder, check the cordova file plugin

    So, if you want to select a public folder, so you can check your files outside of your app, check this link

    https://github.com/apache/cordova-plugin-file#android-file-system-layout

    and choose one with private = no.

    For example:

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dir) {
              dir.getFile("test.pdf", {create: true, exclusive: false}, function (fileEntry) {
                fileEntry.createWriter(function (writer) {
    writer.onwrite = function(evt) {
             console.log("write success");
          };
    
          console.log("writing to file");
             writer.write( pdfOutput );
          }
    
                },function () {
    
    
                  console.log("ERROR SAVEFILE");
    
                });
              });
            });
    

    And your file will be placed in you sdcard/files directory

    For opening it, I supose that you dont have any pdf viewer plugin, so inAppBrowser is the solution for not having to use any, and open it inside your app

    Install inAppBrowser

    And open it:

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dir) {
    
    
              dir.getFile(filename, {create:false}, function(fileEntry) { //EXISTS
                var url = "file:/" + cordova.file.externalDataDirectory + "/test.pdf";
                window.open(url, '_blank');
              }, function() { //NOT EXISTS
    
              });
            });
    

    I haven't tested it, as looks like inAppBrowser dont like too much local files. Please post your results