我正在使用javascript库jsPDF生成pdf。我需要在Windows 8中打开生成的pdf。使用window.open不起作用。我尝试保存文件并打开但没有成功。
如何打开pdf?
[UDPATE]
我正在尝试使用writeBufferAsync保存它,如下所示:
var data = pdf.output('arraybuffer');
Windows.Storage.FileIO.writeBufferAsync(file, data).done(function () {
// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
WinJS.log && WinJS.log("File " + file.name + " was saved.", "sample", "status");
} else {
WinJS.log && WinJS.log("File " + file.name + " couldn't be saved.", "sample", "status");
}
});
});
但是我一直得到异常,我写writeBufferAsync期待数字数组。数据类型是ArrayBuffer。
[更新2]
我尝试了以下内容,bug图片不显示:
var data = pdf.output();
Windows.Storage.FileIO.writeTextAsync(file, data).done(function () {
// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
WinJS.log && WinJS.log("File " + file.name + " was saved.", "sample", "status");
} else {
WinJS.log && WinJS.log("File " + file.name + " couldn't be saved.", "sample", "status");
}
});
});
答案 0 :(得分:1)
将PDF写入文件系统并使用window.open(fileURL,'_ blank')打开它。这就是你应该做的一切。你是怎么试图打开它的?当你做的时候发生了什么?
答案 1 :(得分:1)
我不确定你的函数pdf.output()来自哪里......你自己写的吗?
根据API(https://github.com/mozilla/pdf.js/blob/master/src/display/api.js),您应该引用您的文档,然后致电:
document.getData()
这将返回一个promise,您可以在writeTextAsync函数中使用该promise。 API表示它返回一个类型化数组。我认为它与使用uint8数组加载PDF文档的类型相同。如果你需要转换它,那么就这样做。
如果承诺是问题,那么它应该是这样的:
document.getData().done(function(arg) {
//do something with the results here...
}
答案 2 :(得分:0)
打开或启动文件的最佳方式是使用Windows.System.Launcher.launchFileAsync。您为它提供了StorageFile对象,它完成剩下的工作,打开用户分配给PDF的任何应用程序,或提示用户选择一个。
如果您需要直接在应用中呈现PDF,请使用Windows.Data.Pdf API。请参阅PDF viewer sample。
P.S。如果您将文件写入本地文件系统,并且您知道该文件与云存储无关,那么您就不需要使用CachedFileUpdater。也就是说,如果您正在写入本地appdata或临时appdata,则可以省略该呼叫。