Javascript Detect if Adobe Reader is installed
Asked Answered
M

1

5

We have some PDF forms that don't display correctly in non-Adobe PDF readers (i.e. WebKit's built-in PDF reader does not properly display some proprietary Adobe things). We want to detect when users don't have Adobe's PDF Reader installed and give them a little warning, but I'm having a hard time figuring out how to do it in 2014.

It seems this script worked in 2011. Basically it loops through navigator.plugins and looks for plugins with the name Adobe Acrobat or Chrome PDF Viewer.

 for(key in navigator.plugins) {
      var plugin = navigator.plugins[key];
      if(plugin.name == "Adobe Acrobat") return plugin;
 }

Flash forward to today, Adobe must have changed something, because I have Adobe Acrobat installed, but it doesn't seem to be in navigator.plugins! Where is it now and how do I detect it?

Maximilian answered 5/11, 2014 at 18:53 Comment(3)
I've tested that script here and it works perfectly fine in both Firefox and Chrome. IE however doesn't seem to like it.Conveyancer
Do you have an old version of Adobe Reader? It doesn't even show up in the list of plugins on my computer.Maximilian
Ye. I'm working on it for IE. It seems like the script doesn't detect the IE browser. Oh and I've got Adobe Reader XI installedConveyancer
C
12

Ok I've updated the script and it's working perfectly fine in all browsers now:

<!DOCTYPE HTML>
<html>
    <head>
        <title> New Document </title>
        <script>
        //
        // http://thecodeabode.blogspot.com
        // @author: Ben Kitzelman
        // @license: FreeBSD: (http://opensource.org/licenses/BSD-2-Clause) Do whatever you like with it
        // @updated: 03-03-2013
        //
        var getAcrobatInfo = function() {

            var getBrowserName = function() {
                return this.name = this.name || function() {
                    var userAgent = navigator ? navigator.userAgent.toLowerCase() : "other";

                    if(userAgent.indexOf("chrome") > -1){
                        return "chrome";
                    } else if(userAgent.indexOf("safari") > -1){
                        return "safari";
                    } else if(userAgent.indexOf("msie") > -1 || navigator.appVersion.indexOf('Trident/') > 0){
                        return "ie";
                    } else if(userAgent.indexOf("firefox") > -1){
                        return "firefox";
                    } else {
                        //return "ie";
                        return userAgent;
                    }
                }();
            };

            var getActiveXObject = function(name) {
                try { return new ActiveXObject(name); } catch(e) {}
            };

            var getNavigatorPlugin = function(name) {
                for(key in navigator.plugins) {
                    var plugin = navigator.plugins[key];
                    if(plugin.name == name) return plugin;
                }
            };

            var getPDFPlugin = function() {
                return this.plugin = this.plugin || function() {
                    if(getBrowserName() == 'ie') {
                        //
                        // load the activeX control
                        // AcroPDF.PDF is used by version 7 and later
                        // PDF.PdfCtrl is used by version 6 and earlier
                        return getActiveXObject('AcroPDF.PDF') || getActiveXObject('PDF.PdfCtrl');
                    } else {
                        return getNavigatorPlugin('Adobe Acrobat') || getNavigatorPlugin('Chrome PDF Viewer') || getNavigatorPlugin('WebKit built-in PDF');
                    }
                }();
            };

            var isAcrobatInstalled = function() {
                return !!getPDFPlugin();
            };

            var getAcrobatVersion = function() {
                try {
                    var plugin = getPDFPlugin();

                    if(getBrowserName() == 'ie') {
                        var versions = plugin.GetVersions().split(',');
                        var latest = versions[0].split('=');
                        return parseFloat(latest[1]);
                    }

                    if(plugin.version) return parseInt(plugin.version);
                    return plugin.name
                }
                catch(e) {
                    return null;
                }
            }

            //
            // The returned object
            //
            return {
                browser: getBrowserName(),
                acrobat: isAcrobatInstalled() ? 'installed' : false,
                acrobatVersion: getAcrobatVersion()
            };
        };

        var info = getAcrobatInfo();
        alert(info.browser+ " " + info.acrobat + " " + info.acrobatVersion);
  </script>
 </head>

 <body>

 </body>
</html>
Conveyancer answered 5/11, 2014 at 19:40 Comment(4)
Thanks--on the non-Explorer front, I discovered that while Adobe doesn't install Acrobat into Chrome, it does in Firefox, but the script wasn't detecting it because it was doing if(plugin.name == name), but since the name is Adobe Acrobat NPAPI Plug-In, Version 11.0.09, that was coming up false. The line should probably be changed to if(plugin.name.indexOf(name)>-1). I still don't know why the plugin isn't in Chrome though.Maximilian
Well I've never done anything weird with my Chrome or Adobe Reader and the script above detects it just fine in Chrome. So I find it very strange that it doesn't work for you.Conveyancer
Ah, I thought it wasn't just me because everyone else in the office had the same result. We must have all done the same weird thing to Chrome!Maximilian
Ghehe that'd be something xD But unfortunetely I can't fix what isn't broken here. IE unable to reproduce your problem. So I'm afraid you're on your own with that.Conveyancer

© 2022 - 2024 — McMap. All rights reserved.