Open generated PDF file in Windows8

I am using javascript library jsPDF to generate pdf. I need to open the generated PDF in Windows 8. Using window.open doesn't work. I tried to save the file and open it with no success.

How to open PDF?

[UDPATE]

I am trying to save it using writeBufferAsync like this:

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

      

But I keep getting the exception that I am writing. BufferAsync expects an array of numbers. The data type is ArrayBuffer.

[Update 2]

I tried the following, error images not showing:

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

      

+3


source to share


3 answers


Write the PDF to the file system and use window.open (fileURL, '_blank') to open it. That's all you need to do. How did you try to open it and what happened when you did it?



+1


source


I don't know where your pdf.output () function comes from ... are you writing it yourself?

According to the API ( https://github.com/mozilla/pdf.js/blob/master/src/display/api.js ) you should have a link to your document and then just call:

document.getData()

      



this returns a promise that you can use in the writeTextAsync function. The API says it returns a typed array. I would guess it is the same type as you can load a PDF document with ... a uint8 array. If you need to convert it, do so.

If the problem is promises, then it should look something like this:

document.getData().done(function(arg) {
//do something with the results here...
}

      

+1


source


The best way to open or run a file is Windows.System.Launcher.launchFileAsync . You give it a StorageFile object and it does the rest by opening whatever application the user has assigned to the PDFs or prompting the user to select it.

If you need to make a PDF file directly in your application, use the Windows.Data.Pdf API. See PDF preview sample .

PS If you are writing a file on your local file system in a location that you know is not tied to cloud storage, you don't need to use CachedFileUpdater. That is, if you are writing your local appdata or temp appdata, you can omit this call.

0


source







All Articles