Search code examples
javascriptcordovapdfwindows-8winjs

How to open a local PDF in a Windows 8/Cordova app?


I have an IOS app that I built which now I've been tasked to port over to Windows 8 for use on the Windows tablet. My app downloads files from Dropbox, which gets stored in the Local folder. I can see all of that works fine. I am able to reference images by using ms-appdata:///local/" + filename in the src of my img tag, and I'm even able to play mp4s from the same folder using HTML5 video tags.

My problem is, for my IOS version, I was using Cordova's InAppBrowser to open local PDFs but on this Windows 8 version, it doesn't work.

I am using the following code (filename equals [1]CaseStudy-AC_EN_04.pdf and it does exist on the file system):

 var ref = window.open("ms-appdata:///local/" + filename, '_blank', 'location=no');

And I get the following error in Visual Studio when I run the Simulator

APPHOST9607: The app can't launch the URI at ms-appdata:///local/[1]CaseStudy-AC_EN_04.pdf because of this error: -2147024846.

I've tried switching to WinJS coding methods, even tried loading the PDF in an iFrame but nothing will work. I don't mind kicking the user out to Internet Explorer if I must... I just need some way for the user to see these local PDFs. Is this a permissions issue? I only have a config.xml file and not a app manifest file, so perhaps I'm missing a setting?

Does anyone have experience with this?


Solution

  • In case anyone else has this issue. I was able to do what I wanted with this WinJS code (make sure you include the WinJS framework file)

    //this is just the filename, you can probably skip this step but my filenames are from downloaded files so they could be encoded.
    fileName = decodeURIComponent(fileName);
    
    //get the local folder that contains the downloaded files
    var applicationData = Windows.Storage.ApplicationData.current;
    var localFolder = applicationData.localFolder;
    
    //grab the file and return a promise
    localFolder.getFileAsync(fileName)
    .then(function(file) {
         //launch the file - this command will let the OS use it's default PDF reader - win 8 app 'reader' works great.
         Windows.System.Launcher.launchFileAsync(file);
    });    
    

    That's it.

    @Matthew Corway works fine. But, need aditional attention when the file is within a subfolder as follow exemple:

    var fullPath = "\folderName\fileName.pdf"