How to Display .pdf, .xlsx, .docx Files in a PhoneGap App

Description

Build-in handlers for displaying .pdf, .xlsx, .docx and other file types are usually not available on mobile devices. If your mobile app is built using PhoneGap, you can use the Document Handler PhoneGap plugin to display these file types.

Discussion

The PhoneGap Document Handler plugin allows you to view PDF, MS Office, iWork, RTF, TXT, and image files on a mobile device. If the Document Handler plugin is available, you can call the handleDocumentWithURL Document Handler plugin method to display a document. For example:

function showPDFDocument () {
    var docURL = 'http://downloads.alphasoftware.com/Cordova/documentation/alphaphonegapdoc.pdf';
    var wObj = {};
    wObj.message = "Loading document, please wait...";

    {dialog.object}.showWaitMessage('page',wObj);

    try {
        var onLoadSuccess = function () {
            {dialog.object}.hideWaitMessage();
            alert('Document loaded.');
        };
        var onLoadFail = function () {
            {dialog.object}.hideWaitMessage();
            alert('Document load failed.');
        };

        handleDocumentWithURL(onLoadSuccess, onLoadFail, docURL);

    } catch (e) {
        {dialog.object}.hideWaitMessage();
        alert(e.message);
    }
}

You can add the Document Handler plugin to your PhoneGap application in the PhoneGap Builder. Check the option for Document Handler under Third Party Plugins.

For more information on how to use the Document Handler plugin in your PhoneGap application, watch the following video: