Embed PDF in page and print

I have some code which dynamically loads a PDF document into a web page by setting a container's innerHTML to the returned string of this function:

function getPdfString(url) {
    return '<object data="' + url + '" type="application/pdf" classid="clsid:ca8a9780-280d-11cf-a24d-444553540000" style="width:100%;height:600px"></object>';
}

In IE with the Adobe Reader plugin installed (as determined by the code that detects the Adobe ActiveX at PDFObject), my code inserts this HTML into a hidden container, puts a reference to the object element into el , and then runs this code (Repeater is a custom class):

log("** start repeater **");
var r = _repeater = new Repeater(function() {
    try {
        var delta = timeInterval();
        log("iteration - " + delta + "ms");

        el.gotoFirstPage(); //throws exceptions until the PDF is loaded

        log("** assuming success, stop **");
        r.stop();
        r = undefined;

        setTimeout(function() {
            el.print(); //should succeed, can't tell because it doesn't throw or return anything
        }, 100);
    } catch(e) { }
}, 0, 100);

This is very convoluted, but necessary because there's no way to tell when the PDF is loaded, nor whether or not el.print() succeeded. It took me a long time to figure out, but it seems to work well in IE7 and IE8. IE9 has been hit and miss, usually working on my local machine (which runs IIS7.5), but sometimes not. IE9 has never worked when the site is running on my test server, which runs IIS6 out of necessity. I don't know if the version of IIS that I am running is causing my issue, but judging from the Fiddler logs, I doubt it.

I have been poring over Fiddler, making small tweaks here and there to see if anything makes a difference. So far, nothing has. The only difference that I can see is the Server header.

I found that the classid attribute is needed by IE7 and IE8; otherwise, they will make multiple requests for the PDF, and often fail to load it. It also significantly improves IE9's caching behavior.

The PDF is slightly different each time it is acquired. I'm not currently saving it to a temporary file or anything, though I could if it is absolutely necessary (so I could re-send the same PDF in a subsequent request).

The response is being gzip encoded, but I have the same problem whether it is enabled or not.

I have noticed that when the problem occurs, terminating AcroRd32.exe sometimes fixes the issue temporarily.

Side note: Firefox and Opera use the same HTML in an in-page popup which embeds the PDF. This works perfectly fine. (The Adobe Reader NPAPI plugin doesn't have a print() method on it that I have been able to find, sadly, so the popup instructs users to click the embedded view's Print button)

Nothing is stopping me from trying other methods of embedding such as an iframe , but I had some weird issues with it when I first tried it (can't remember what they were now, after all this mess).

I think that's everything I know about the problem right now...


This seems to be a problem specifically with Adobe Reader and the IE plugin. I've found a few forum threads that indicate this is a common, reproducible error (http://forums.adobe.com/thread/758489).

The solution seems to be using an iFrame over an <object> / <embed> tag.

链接地址: http://www.djcxy.com/p/36410.html

上一篇: Rails Bootstrap CSS问题

下一篇: 将PDF嵌入页面并打印