How to Launch a PDF from a UWP (Universal Windows Platform) Web Application

后端 未结 3 1240
小蘑菇
小蘑菇 2020-12-21 14:15

I\'ve converted an existing web application (HTML5, JS, CSS, etc.) into a Windows UWP app so that (hopefully) I can distribute it via the Windows Store to Surface Hubs so it

相关标签:
3条回答
  • 2020-12-21 14:54

    LaunchFileAsync is the right API to use here. You can't launch a file directly from the install directory because it is protected. You need to copy it first to a location that is accessible for the other app (e.g. your PDF viewer). Use StorageFile.CopyAsync to make a copy in the desired location.

    Official SDK sample: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/AssociationLaunching

    0 讨论(0)
  • 2020-12-21 14:57

    I just thought I'd add a variation on this answer, which combines some details from above with this info about saving a blob as a file in a JavaScript app. My case is that I have a BLOB that represents the data for an epub file, and because of the UWP content security policy, it's not possible simply to force a click on a URL created from the BLOB (that "simple" method is explicitly blocked in UWP, even though it works in Edge). Here is the code that worked for me:

    // Copy BLOB to downloads folder and launch from there in Edge
    // First create an empty file in the folder
    Windows.Storage.DownloadsFolder.createFileAsync(filename,
        Windows.Storage.CreationCollisionOption.generateUniqueName).then(
        function (file) {
        // Open the returned dummy file in order to copy the data to it 
        file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (output) {
            // Get the InputStream stream from the blob object 
            var input = blob.msDetachStream();
            // Copy the stream from the blob to the File stream 
            Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then(
            function () {
                output.flushAsync().done(function () {
                    input.close();
                    output.close();
                    Windows.System.Launcher.launchFileAsync(file);
                });
            });
        });
    }); 
    

    Note that CreationCollisionOption.generateUniqueName handles the file renaming automatically, so I don't need to fiddle with performance.now() as in the answer above.

    Just to add that one of the things that's so difficult about UWP app development, especially in JavaScript, is how hard it is to find coherent information. It took me hours and hours to put the above together from snippets and post replies, following false paths and incomplete MS documentation.

    0 讨论(0)
  • 2020-12-21 15:08

    You will want to use the PDF APIs https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/PdfDocument/js

    https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/PdfDocument/js/js/scenario1-render.js

    Are you simply just trying to render a PDF file?

    0 讨论(0)
提交回复
热议问题