2016-05-12 164 views
1

我试图为我的应用程序生成一个PDF发票,我可以到'console.log(“写入成功”);''行'但我无法找到该文件。请帮忙!!!我怎样才能将它保存到我的手机中的文件夹?jsPDF和cordova无法查看pdf文件

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); 
+0

你可以发布console.log(fileSystem.root.fullPath)的结果吗? – Del

+0

它的到来/ – Philomath

回答

1

您正在将文件保存在系统的根路径中。

如果你要选择的文件夹,选中cordova file plugin

所以,如果你想选择一个公共文件夹,这样你就可以检查你的应用程序之外的文件,检查此链接

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

并选择一个private = no。

例如:

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"); 

      }); 
      }); 
     }); 

和您的文件将被放置在你的SD卡/ files目录

为了打开它,我supose你不要有任何PDF浏览器插件,所以inAppBrowser是解决方案对于没有使用任何,并打开您的应用程序内

安装inAppBrowser

并打开它:

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 

      }); 
     }); 

我还没有测试它,因为看起来像在AppBrowser不喜欢太多的本地文件。请发表您的结果

+0

我相信没有像'cordova.file.file'这样的东西请纠正我,如果我错了。 – Gandhi

+0

嘿,谢谢你!有效。我的pdf被保存在我的应用程序目录中。感谢一百万。你也可以建议一旦创建它,​​我该如何打开这个PDF文件?我给了cordova.file.externalDataDirectory。当然file.file只是一个例子,我想 – Philomath

+0

好吧,我会在1小时内发布它,因为我不在家 – Del

0

我觉得下面的线可能是问题,“window.requestFileSystem(LocalFileSystem.PERSISTENT,0,函数(文件系统){})”

在一些我读的帖子,有人提到requestfilsystem方法和LocalFileSystem.PERSISTENT参数在android中不起作用,除非设备是root用户。

我使用 - “window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,successCallback,errorCallback);”

我建议你看看这个link,我已经在Android和iOS设备上发布了相同的文件创建代码。

此外,如果你想在你选择的应用程序中打开PDF,你可以使用cordova file opener插件。你可以看看下面的文件开启器示例代码link

+0

尝试inappbrowser后会尽快回复 – Philomath

+0

@删除它的不工作..没有错误被抛出,但文件没有打开 – Philomath

+0

@ user2281415你试过fileopener示例? – Gandhi