These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

postMessage to HTML hostContainer not working suitably

LionArt
Registered: Jan 7 2011
Posts: 4

Hello,
 
I'm injecting a JavaScript code to a PDF file with LCES 2 product, the script contains :
hostContainer.postMessage(["OK"]);
but unded IE8 the message is not catch the first time, it's catch only the second time and after or when I reload the page.
I have a html div; named pdfDiv; in which I injecting this script :
 
document.getElementById("pdfDiv").innerHTML = " ";
 
pdfObject = document.getElementById("pdfObject");
pdfObject.messageHandler = {
onMessage : function(aMessage) {
alert(aMessage);
},
onError : function(error, aMessage) {
alert(aMessage);
}
};
 
Under chrome it is always catch and under Opera the message is never catch.
 
Am I proceeding correctly ? Does someone knows this problems ?

My Product Information:
Acrobat Standard 9.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You've got a timing issue. The message handler on the HTML side cannot be added until the PDF if fully loaded. Delay adding the message handlers. For example, one way to do this is to wait until some user action. Users will typically wait until the content is loaded until doing something. Or you can just use a timeout.

I've got an example that uses a timeout here:
http://www.windjack.com/WindJack/Browser2PDF/brwsr2acroJS.htm

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

LionArt
Registered: Jan 7 2011
Posts: 4
Thank you very much I will try this. But what I do is :
- Launch silent print of the document.
- postMessage to JavaScript.

and all of that automatically, it means when PDF is loaded in the page with "safe path". So I have a chance to miss the message. True ? Can I add a handler for a print event inside the PDF ? And which HTML event can I use because I use the same object to load a list of PDFs to print ?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Automating the process is much more difficult. It would be helpful if there was an event on the HTML side to let you know it's all setup and working. But I don't know if any such event exists, and is supported across all the different browsers.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

LionArt
Registered: Jan 7 2011
Posts: 4
Thanks. Now I'm using "readyState" property to know if the PDF is completely downloaded or not.
What I'm doing now is :
1 - Loading the PDF.
2 - Adding messageHandler to the PDFObject when readyState is equal to 4.
3 - Post message from BrowserJS to AcrobatJS.
4 - Catch the message Acrobat side.
5 - Print.
6 - Post message from AcrobatJS to BrowserJS .

But what I'm doing is not working and I have no message error. The document is not printed.

In my BorwserJS I have :

if (pdfObject.readyState == 4)
{
pdfObject.postMessage(["print"]);
var msgHandlerObject = new Object();
msgHandlerObject.onMessage = onPDFMessage;
msgHandlerObject.onError = ondPDFError;
pdfObject.messageHandler = msgHandlerObject;
pdfObject.postMessage(["print"]);
}

And in my PDF I have the following code :

function onMessageReceived(message) {
if (message.length == 1) {
switch(message[0]) {
case "print" :
this.print();
this.hostContainer.postMessage(["documentPrinted"]);
break;
default : this.hostContainer.postMessage(["unknowMessage"]);
}
}
else {
this.hostContainer.postMessage(["messageOverflow"]);
}
}

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

function onErrorReceived(error, message) {
this.hostContainer.postMessage([error]);
}

var msgHandlerObject = new Object();
msgHandlerObject.onMessage = onMessageReceived;
msgHandlerObject.onDisclose = onDiscloseHandler;
msgHandlerObject.onError = onErrorReceived;
this.hostContainer.messageHandler = msgHandlerObject;

}

Is the last line correct ?
Instead, if I write "this.messageHandler = msgHandlerObject; " I have "MessageDocNotDisclosedError".

I'm using the correct syntax to handle the message from BrowserJS ?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
If you want to print the PDF You can use the AxAcroPDFLib interface. It's much more straight forward than using HostContainer.For example, in the HTML JS:

pdfObject.Print();

This is all you need.
Except that printing is a protected operation. It may not be easy to get rid of the popup print dialog.

If you really want to use the Host Container object I'd suggest building a simple sample HTML page and PDF. Get the hang of using the Host Container first, and then add in the functionality you want to implement.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

LionArt
Registered: Jan 7 2011
Posts: 4
Thank you very much. The code I wrote just before is working but passing the message from BrowserJS to AcrobastJS is not working. I already automated printing a list of PDF files using autorpint when PDF opens then loading the next but I encountered a problem when it is already available in the browser cache.

And the main goal is to print without AxAcroPDF, that is why I'm using AcrobatJS and BrowserJS. And for the print popup the solution is to make silent print and add the URL where the file is hosted to the safe path of my Acrobat Reader.

But please can you correct my code ? And where can I find an example code where BrowserJS communicates with AcrobatJS ? ( I haven't Acrobat Pro to read code included in your example, can you post inner code ? )
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Well, the thing is, I still think you have a timing issue. I think you need to create a test that does not print. It just sends messages back and forth. Then add the print functionality. However I also think that the print will cause further timing problems. The print is non-blocking. Meaning that the print function just tells acrobat to stuff the print job into the que. There's a lot going on the background that could cause delays that may screw up registering the message handlers on the HTML side. That's the side you need to delay.

Here's an early example provided by Adobe. I think I originally pulled from the 7.05 SDK. But I can't find the original now. Copy the HTML and PDF to your own server. I'll take it down if I see a lot of activity on mine.

The example has buttons that setup each handler individually. This will give you a feel for how the sequence needs to proceed.

http://www.windjack.com/WindJack/Browser2PDF/bidirectional.php

You must have Acrobat Pro to do this. It's almost impossible to debug the code without direct access to the scripts. This is something you have to do.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

sorvik
Registered: May 27 2011
Posts: 3
Hi,
I tryed to use "IE Browser/Acrobat Integration" sample published on windjack and it works fine on older versions of IE, but i installed latest Acrobat reader and IE9 and it crashes (no error messages but simple crash of IE) when it comes to call postMessage method to pass somthing in PDF. As i can guess it is related to HTML 50 implementation of postMessage? Any ideas on how to call it old way or my be there is new substitude method in AcroScript?
Regards,
Victor
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Thanks for the heads up, I'll check it out

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

sorvik
Registered: May 27 2011
Posts: 3
hi,
any updates to this issue? My main goal to send messages to PDF document from windows appliaction, i tryed to send messages directly to Acrobat activex but seem can't to find how to create array to pass to this function, regular SAFEARRAY does not work. If now IE will stop working with it i need to find some good and stable alternative to using IE webcontrol.
Regards,
Victor
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
It looks like this is related to the 64 bit nature of IE 9 and later. The Acrobat 9 Active X component does not seem to be integrating well with IE. I remember seeing a note somewhere that issues with 64 bit IE were fixed in Acrobat X. Although I don't remember which issues and I haven't tested the Host Container in X on a 64bit system.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

sorvik
Registered: May 27 2011
Posts: 3
But I tryied it on 32 bit version of windows 7.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I'm not sure then. This is one of those things that is easy to fall out of compatibility. The host container is supposed to follow a browser standard for "Object" interfaces, but each browser is different, or interprets the spec differently. And it changes with different versions. If you asked Adobe they would say that not enough customers use it for them to spend time supporting it:( I get a lot of that from them. Also, I haven't used it in a project for a while because it's not very reliable.

My only suggestion is to setup a test PDF and Web page that gives you individual control over each part of the entire process, From the setup to posting and recieving messages. Figure out exactly where it's failing. And test to see if it's a timing issue. IE, maybe things are being setup too quickly. There is a know issue with trying to settup the HTML side of the message handler before the Acrobat add-in is loaded in the browser.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script