无法在IE8中显示PDF对象

时间:2014-04-21 05:03:52

标签: javascript pdf internet-explorer-8 pdfobject

我想请求帮助来解决ASP.Net MVC应用程序中面临的IE浏览器不兼容问题。该应用程序的一个页面包含一个显示PDF的链接。在IE8中,页面显示错误(" Internet Explorer无法显示此页面"或空白页面)。但是,我可以在Google Chrome,Mozilla Firefox和IE9中访问pdf。

实际上,我需要在IE8中显示PDF

如果有人在此之前遇到类似问题或有任何决议,请您帮我们解决此问题?我尝试了几个选项,但无法解决它。

HTML

<div id="pdf1" class="message_details_pdf"></div>

Java脚本代码

 var myPDF = new PDFObject({
            url: 'my_pdf_url',
            pdfOpenParams: {
                view: 'Fit',
                scrollbars: '0',
                toolbar: '0',
                statusbar: '0',
                navpanes: '0'
            }
        }).embed("pdf1");

1 个答案:

答案 0 :(得分:0)

我遇到了与你类似的问题,但我正在与IE11作斗争。我在pdfobject.js中添加了一些代码来解决我的问题。我不知道这是否适用于IE8,但我认为我的mod可以帮助你。

通过查找&#34; //#&#34;来搜索mod。没有引号,在下面的代码中。希望这会有所帮助。

var PDFObject = function (obj)
{
    if (!obj || !obj.url) { return false; }

    var pdfobjectversion = "1.2",
        //Set reasonable defaults
        id = obj.id || false,
        width = obj.width || "100%",
        height = obj.height || "100%",
        pdfOpenParams = obj.pdfOpenParams,
        url,
        pluginTypeFound;

    /* ----------------------------------------------------
    Supporting functions
    ---------------------------------------------------- */

    //Tests specifically for Adobe Reader (aka Acrobat) in Internet Explorer
    var hasReaderActiveX = function ()
    {
        var axObj = null;
        if (window.ActiveXObject)
        {
            axObj = new ActiveXObject("AcroPDF.PDF");
            //If "AcroPDF.PDF" didn't work, try "PDF.PdfCtrl"
            if (!axObj) { axObj = new ActiveXObject("PDF.PdfCtrl"); }
            //If either "AcroPDF.PDF" or "PDF.PdfCtrl" are found, return true
            if (axObj !== null) { return true; }
        }
        //If you got to this point, there's no ActiveXObject for PDFs
        return false;
    };


    //Tests specifically for Adobe Reader (aka Adobe Acrobat) in non-IE browsers
    var hasReader = function ()
    {
        var i,
            n = navigator.plugins,
            count = n.length,
            regx = /Adobe Reader|Adobe PDF|Acrobat/gi;
        for (i = 0; i < count; i++) { if (regx.test(n[i].name)) { return true; } }
        return false;
    };


    //Detects unbranded PDF support
    var hasGeneric = function ()
    {
        var plugin = navigator.mimeTypes["application/pdf"];
        return (plugin && plugin.enabledPlugin);
    };

    //# ===============================================
    //# taken from http://www.quirksmode.org/js/detect.html
    //# ===============================================

    var BrowserDetect = {
        init: function () {
            this.browser = this.searchString(this.dataBrowser) || "Other";
            this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "Unknown";
        },
        searchString: function (data) {
            for (var i = 0; i < data.length; i++) {
                var dataString = data[i].string;
                this.versionSearchString = data[i].subString;

                if (dataString.indexOf(data[i].subString) !== -1) {
                    return data[i].identity;
                }
            }
        },
        searchVersion: function (dataString) {
            var index = dataString.indexOf(this.versionSearchString);
            if (index === -1) {
                return;
            }

            var rv = dataString.indexOf("rv:");
            if (this.versionSearchString === "Trident" && rv !== -1) {
                return parseFloat(dataString.substring(rv + 3));
            } else {
                return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
            }
        },

        dataBrowser: [{string: navigator.userAgent,subString: "Chrome",identity: "Chrome"}, {string: navigator.userAgent,subString: "MSIE",identity: "Explorer"}, {string: navigator.userAgent,subString: "Trident",identity: "Explorer"}, {string: navigator.userAgent,subString: "Firefox",identity: "Firefox"}, {string: navigator.userAgent,subString: "Safari",identity: "Safari"}, {string: navigator.userAgent,subString: "Opera",identity: "Opera"}]

    };

    //# END===============================================

    //Determines what kind of PDF support is available: Adobe or generic
    var pluginFound = function ()
    {
        var type = null;
        var versione = null;

        //# ===============================================
        //# Start browser detecting
        //# ===============================================
        BrowserDetect.init();

        if (hasReader() || hasReaderActiveX())
        {
            type = "Adobe";
            version = null;

        } else if (hasGeneric())
        {
            type = "generic";
            version = null;

        //# ===============================================
        //# ...check if explorer
        //# ===============================================
        } else if (BrowserDetect.browser == 'Explorer')
        {
            type = "IE";
            version = BrowserDetect.version;
        }

        return {'type': type,'version': version};
    };


    //If setting PDF to fill page, need to handle some CSS first
    var setCssForFullWindowPdf = function ()
    {
        var html = document.getElementsByTagName("html");
        if (!html) { return false; }

        var html_style = html[0].style,
            body_style = document.body.style;

        html_style.height = "100%";
        html_style.overflow = "hidden";
        body_style.margin = "0";
        body_style.padding = "0";
        body_style.height = "100%";
        body_style.overflow = "hidden";
    };


    //Creating a querystring for using PDF Open parameters when embedding PDF
    var buildQueryString = function (pdfParams)
    {
        var string = "",
            prop;

        if (!pdfParams) { return string; }

        for (prop in pdfParams) {
            if (pdfParams.hasOwnProperty(prop)) {
                string += prop + "=";
                if (prop === "search") {
                    string += encodeURI(pdfParams[prop]);
                } else {
                    string += pdfParams[prop];
                }
                string += "&";
            }
        }

        //Remove last ampersand
        return string.slice(0, string.length - 1);
    };


    //Simple function for returning values from PDFObject
    var get = function (prop)
    {
        var value = null;

        switch (prop) {
        case "url":
            value = url;
            break;
        case "id":
            value = id;
            break;
        case "width":
            value = width;
            break;
        case "height":
            value = height;
            break;
        case "pdfOpenParams":
            value = pdfOpenParams;
            break;
        case "pluginTypeFound":
            value = pluginTypeFound;
            break;
        case "pdfobjectversion":
            value = pdfobjectversion;
            break;
        }

        return value;
    };


    /* ----------------------------------------------------
    PDF Embedding functions
    ---------------------------------------------------- */
    var embed = function (targetID)
    {
        if (!pluginTypeFound) { return false; }
        var targetNode = null;

        if (targetID)
        {
            //Allow users to pass an element OR an element's ID
            targetNode = (targetID.nodeType && targetID.nodeType === 1) ? targetID : document.getElementById(targetID);
            //Ensure target element is found in document before continuing
            if (!targetNode) {
                return false;
            }
        } else
        {
            targetNode = document.body;
            setCssForFullWindowPdf();
            width = "100%";
            height = "100%";
        }

        //# ===============================================
        //# ...and, if explorer found, write an iframe instead of an object
        //# ===============================================
        if (pluginTypeFound == 'IE')
        {
            targetNode.innerHTML = '<iframe type="application/pdf" width="' + width + '" height="' + height + '" src="' + url + '"><p>nineoclick</p></iframe>';
        } else
        {
            targetNode.innerHTML = '<object data="' + url + '" type="application/pdf" width="' + width + '" height="' + height + '" style="z-index:800 !important;"></object>';
        }
        return targetNode.getElementsByTagName("object")[0];
    };

    //The hash (#) prevents odd behavior in Windows
    //Append optional Adobe params for opening document
    url = encodeURI(obj.url) + "#" + buildQueryString(pdfOpenParams);
    plugin = pluginFound();
    pluginTypeFound = plugin.type;
    pluginVersionFound = plugin.version;
    this.get = function (prop) {
        return get(prop);
    };
    this.embed = function (id) {
        return embed(id);
    };
    return this;
};