Search code examples
javascriptpdfundefinedpostmessage

PDF postMessage is undefined


Note - this seems to document the same issue I am experiencing regarding "postMessage" being undefined, but no answer is provided there.

I am new to working with PDF files. I'm trying to fix a bug in an existing application. The application displays a form in the browser, allows the user to edit, and then uses Adobe's JavaScript postMessage method to post back the user's changes to the server. For some reason, the PDF object does not seem to have the method postMessage defined. The pdfObject is found by name fine, and it has many, many properties (attributes is a node map, baseURL is the URL the user used to navigate to the PDF, childNodes and contentDocument are set, there are many onXX methods (e.g. onBeforeCut, onActivate, etc.), but myPdfObj.postMessage is undefined. The PDF displays fine and can be edited, but when the app tries to call the undefined method postMessage, the error "ERROR: Target document not disclosed" appears on the screen. Any idea what might be going on?

Note - It looks like our staging site does still work EVEN THOUGH postMessage is undefined. I have no idea why. However, locally hosted site does not work.

Here's a little more info per questions posted in comments:

I think it should have a postMessage function because our code is calling that method, and it isn't declared anywhere, and I find evidence from posts on the net (e.g. here and here) that others are calling that method on PDF objects, so I think it's something Adobe provides.

The browser is IE II.

Here is the code causing the error - when this button is clicked

<button id="useraction_savedraft" onclick="saveDraft();">Save Draft</button> 

This JavaScript is called:

function saveDraft() {
    $('#keepAsDraft').val('true');
    sendMessage(["submitForm"]);
}

var pdfObject = null;
function sendMessage(aMessage) {

    if (pdfObject == null) {
        pdfObject = document.getElementById("pdfForm");

        if (typeof(pdfObject) == "undefined" || typeof (pdfObject.postMessage) == "undefined") {
            statusErrorMessage("ERROR: Unable to initialize pdfForm.  Your browser may not be compatible with PDF inline-editing.");
            return;
        }

        pdfObject.messageHandler = {
            onMessage: function(msg) {
                if (msg[0]=="saveFdf") {
                    $("#fdf").val(msg[1]);
                    $("#entryForm").submit();
                }

                else if (msg[0]=="saveXFA") {
                    $("#fdf").val("");
                    $("#xfa").val(msg[1]);
                    $("#entryForm").submit();
                }
            },

            onError: function(error, msg)
            {
                statusErrorMessage("ERROR: " + error.message);
                return;
            }
        };
    }

    pdfObject.postMessage(aMessage);
}

The scripts that run when onMessage and onError are called are js functions that are embedded in the PDF file itself (we use a 3rd party library to embed the scripts). The functions are called myOnMessage and myOnError. We also define a myOnDisclose method in the same JavaScript, and at the end of the embedded script, it uses this logic to assign the methods to the container's messageHandler:

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

this.hostContainer.messageHandler = msgHandlerObject;

Many thanks!


Solution

  • Well, I'm not sure how to explain it, but it seems that it's okay for the method "postMessage" to be undefined. It turns out that the reason the callback wasn't working was that I was making a change to how the scripts that it calls get embedded in the PDF server-side, and wasn't embedding them right anymore. When I fix that, it all works, although the postMessage is still undefined so I'm not sure why/how it works.