在iFrame中将postMessage转换为PDF [英] postMessage to PDF in an iFrame

查看:244
本文介绍了在iFrame中将postMessage转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况.

我有一个带有嵌入式PDF表单的网页.我们使用一个基本的对象标签(嵌入FF中)来加载PDF文件,如下所示:

I had a webpage with an embedded PDF form. We used a basic object tag (embed in FF) to load the PDF file like this:

<object id="pdfForm" height="100%" width="100%" type="application/pdf" data="..url"></object>

在此网页上有一个保存HTML"按钮,该按钮将触发一些Javascript,该Javascript使用嵌入对象的postMessage API执行嵌入在PDF中的javascript.基本上,该代码如下所示:

On this webpage was an Html Save button that would trigger some Javascript which used the postMessage API of the embedded object to execute javascript embedded in the PDF. Basically, that code looked like this:

function save() {
    sendMessage(["submitForm"]);
}

function sendMessage(aMessage) {
    pdfObject = document.getElementById("pdfForm");

    if (typeof(pdfObject) == "undefined")
      return;

    if (typeof (pdfObject.postMessage) == "undefined")
      return;

    pdfObject.postMessage(aMessage);
}

这一切都工作得很好.

除了我们在Firefox中遇到问题外,我们需要使用iFrame(而不是object标签)嵌入PDF.因此,现在,使用以下代码嵌入PDF:

Except we ran into an issue with Firefox so that we need to embed the PDF using iFrame, instead of the object tag. So now, the PDF is embeded using this code:

<iframe id="pdfWrapper" src="..someUrl" width="100%" height="800px" frameborder="0"></iframe>

不幸的是,使用此代码,用于发布消息的javascript不再起作用,并且我无法真正弄清楚如何访问pdf对象,以便我可以访问postMessage api.

Unfortunately, with this code, the javascript for posting a message no longer works, and I can't really figure out how to get access to the pdf object anymore so that I can access the postMessage api.

使用小提琴手或chome javascript调试器,很明显,在iframe中,浏览器会自动生成一个embed标签(而不是object标签),但这不能让我访问postMessage API.这是我正在尝试的代码,它不起作用:

Using fiddler or the chome javascript debugger, it is clear that within the iframe, the browser is automatically generating an embed tag (not an object tag), but that does not let me access the postMessage API. This is the code I'm trying which doesn't work:

function sendMessage(aMessage) {

        var frame = document.getElementById("pdfWrapper");
        var doc = null;

        if (frame.contentDocument)
           doc = frame.contentDocument;
        else
        if (frame.contentWindow)
           doc = frame.contentWindow.document;
        else
        if (frame.document)
           doc = frame.document;

        if (doc==null || typeof(doc) == "undefined")
          return;

        var pdfObject = doc.embeds[0];

        if (pdfObject==null || typeof (pdfObject.postMessage) == "undefined")
          return;

        pdfObject.postMessage(aMessage);
    }

对此有任何帮助吗?很抱歉,这个问题很久.

Any help on this? Sorry for the long question.

有人要求我提供代码示例,以便人们可以测试消息传递是否有效.本质上,您只需要嵌入此javascript的任何PDF.

I've been asked to provide samples in code so that people can test whether the messaging works. Essentially, all you need is any PDF with this javascript embedded.

function myOnMessage(aMessage) {
    app.alert("Hello World!");
}

function myOnDisclose(cURL, cDocumentURL) {
    return true;
}

function myOnError(error, aMessage) {
    app.alert(error);
}

var msgHandlerObject = new Object();
msgHandlerObject.onMessage = myOnMessage;
msgHandlerObject.onError = myOnError;
msgHandlerObject.onDisclose = myOnDisclose;
msgHandlerObject.myDoc = this;

this.hostContainer.messageHandler = msgHandlerObject;

我意识到您需要Acrobat Pro才能使用javascript创建PDF,因此为了简化操作,我在以下URL上发布了示例代码(工作和非工作场景):

I realize you need Acrobat pro to create PDFs with javascript, so to make this easier, I posted sample code--both working and non working scenarios--at this url: http://www.filedropper.com/pdfmessage

如果使用Windows,则可以下载zip并将其解压缩到/inetpub/wwwroot,然后将浏览器指向works.htm或fail.htm.

You can download the zip and extract it to /inetpub/wwwroot if you use Windows, and then point your browser to either the works.htm or fails.htm.

感谢您提供的任何帮助.

Thanks for any help you can give.

推荐答案

对于Internet Explorer,使用:

For Internet Explorer, use:

<object id="pdfForm" type="application/pdf" data="yourPDF.pdf"></object>

对于Firefox,使用:

For Firefox, use:

<embed id="pdfForm" type="application/pdf" src="yourPDF.pdf"></embed>

这篇关于在iFrame中将postMessage转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆