【问题标题】:jsPDF and cordova Cant view the pdf filejsPDF和cordova无法查看pdf文件
【发布时间】:2016-05-12 11:30:14
【问题描述】:

我正在尝试为我的应用程序生成 PDF 发票,我可以访问 'console.log ("write success");' 行但我只是找不到文件。请帮忙!!!如何将其保存到手机中的文件夹中???

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

【问题讨论】:

  • 你能发布console.log(fileSystem.root.fullPath)的结果吗?

标签: cordova cordova-plugins jspdf


【解决方案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");

            });
          });
        });

你的文件会放在你的 sdcard/files 目录中

为了打开它,我假设你没有任何 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

          });
        });

我还没有测试过,因为看起来 inAppBrowser 不喜欢太多的本地文件。请发布您的结果

【讨论】:

  • 我相信没有像'cordova.file.file'这样的东西如果我错了请纠正我。
  • 嘿,谢谢!有效。我的 pdf 保存在我的应用程序目录中。谢谢一百万。您能否建议我如何在创建此 pdf 后打开它?我给了cordova.file.externalDataDirectory。当然 file.file 只是我想的一个例子
  • 好的,我不在家,1小时后发
  • 你想怎么打开它?你有 pspdfkit 或 pdftron 之类的 pdf 查看器插件吗?
  • @user2281415 已编辑我的答案以提供有关 pdf 查看的更多信息。
【解决方案2】:

我觉得下面这行可能是问题所在,"window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {})"

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

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

使它工作

我建议你看看这个link,我在其中发布了用于在 Android 和 iOS 设备中创建文件的相同代码。

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

【讨论】:

  • 尝试inappbrowser后会尽快回复
  • @Del 它不工作..没有错误被抛出,但文件没有打开
  • 尚未尝试..我很困惑如何将 uri 提供给文件..cdr.nativeURL +“test.pdf”,即使在应用程序浏览器方法中我获取文件未找到错误。 .
  • @user2281415 我在帖子中的第一个链接包含有关 cdr.nativeURL 的详细信息。有任何错误跟踪吗?您是否在设备上进行了测试?
  • 实际上文件打开器插件我没有收到任何成功或错误消息
猜你喜欢
  • 2017-12-20
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多