views:

214

answers:

1

I was recently searching for a way to call the print function on a PDF I was displaying in adobe air. I solved this problem with a little help from this fellow, and by calling postMessage on my PDF like so:

//this is the HTML I use to view my PDF
<object id="PDFObj" data="test.pdf" type="application/pdf"/>

...
//this actionscript lives in my air app
var pdfObj:Object = htmlLoader.window.document.getElementById("PDFObj");
pdfObj.postMessage([message]);

I've tried this in JavaScript as well, just to be sure it wasn't adobe sneaking in and helping me out...

var obj = document.getElementById("PDFObj");
obj.postMessage([message]);

Works well in JavaScript and in ActionScript.

I looked up what the MDC had to say about postMessage, but all I found was window.postMessage.

Now, the code works like a charm, and postMessage magically sends my message to my PDF's embedded JavaScript. However, I'm still not sure how I'm doing this.

I found adobe talking about this method, but not really explaining it:

HTML-PDF communication basics
JavaScript in an HTML page can send a message to JavaScript in PDF content by calling the postMessage() method of the DOM object representing the PDF content.

Any ideas how this is accomplished?

+2  A: 

"postMessage" is essentially one half of a publish/subscribe model for JavaScript.

You can post any message you like, but it relies on something listening for that message event. So your postMessage is essentially you throwing an event over the fence, hoping that something is waiting on the other side to do something with the event. On the other side of the fence is (hopefully) an event listener like this:

window.addEventListener("message", doSomethingWithTheMessage, false);

function doSomethingWithTheMessage(event) {
    alert("Do Something!");
}

More information here: https://developer.mozilla.org/en/DOM/window.postMessage

In your specific example, when you embed an object such as a PDF, Flash or something along those lines, they may well listen for events in exactly this way.

Sohnee