How to know if browser has PDF viewer or not?

前端 未结 4 2242
再見小時候
再見小時候 2021-02-13 04:38

I am viewing PDF into iframe. It works fine. But some of the client don\'t able to see it in IE. They get it as download option.

How can I identify if browser has pdf v

4条回答
  •  暖寄归人
    2021-02-13 04:47

    I tried following solution by taken help from عبد النور التومي, which helps for any pdf viewer in chrome and mozilla

        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 || userAgent.indexOf("trident") > -1) { return "ie"; }
                    else if (userAgent.indexOf("firefox") > -1) { return "firefox";}
                    return userAgent;
                }();
            };
    
            var getActiveXObject = function (name) {
                try { return new ActiveXObject(name); } catch (e) { }
            };
    
            var getNavigatorPlugin = function (name) {
                try {
                    for (key in navigator.plugins) {
                        var plugin = navigator.plugins[key];
                        if (plugin.name.toLowerCase().indexOf(name) > -1) { return plugin; }
                    }
                } catch (e) {
    
                }
    
            };
    
            var getPDFPlugin = function () {
                return this.plugin = this.plugin || function () {
                    if (getBrowserName() == 'ie') {
                        return getActiveXObject('AcroPDF.PDF') || getActiveXObject('PDF.PdfCtrl');
                    }
                    else {
                        return getNavigatorPlugin('adobe acrobat') || getNavigatorPlugin('pdf') || getNavigatorPlugin('foxit reader');  // works for all plugins which has word like 'adobe acrobat', 'pdf' and 'foxit reader'.
                    }
                }();
            };
    
            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;
                }
            };
    
            return {
                browser: getBrowserName(),      // Return browser name
                acrobat: isAcrobatInstalled() ? true : false,   // return pdf viewer is enabled or not
                acrobatVersion: getAcrobatVersion()  // reurn acrobat version for browser
    
    
       };
    }
    

    And I got following error for IE, Though I have pdf viewer installed in my browser.: enter image description here

    Then I solved it by this link.

    Then I add new condition for IE 11 trident and now it works fine. Also I added option to check for foxit reader. you can add another pdf reader name also in or condition.

提交回复
热议问题