2016-05-12 167 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