Hi -
I have a need to print a PDF programmatically, via Javascript. The PDF is embedded in a Web page. I use postMessage to communicate with the PDF.
I've created a simple HTML page with an embedded PDF, and a Javascript link to call my "Print" function. Here's a code snippet:
<body>
<a href="javascript:doPrint();">Print</a>
<object id="PDFObj" data="testPDF.pdf" type="application/pdf" width="400" height="400" />
</body>
In the HTML document's HEAD, I've added the following Javascript which posts the message to the PDF:
<script language="JavaScript">
function doPrint() {
pdfObject = document.getElementById("PDFObj");
alert("Hello from Javascript...");
pdfObject.postMessage(["Print", "Print"]);
}
</script>
Finally, I've added an Acrobat Javascript function in the PDF document to listen for the postMessage event:
function myOnMessage(aMessage)
{
app.alert("Hello from the PDF");
// print routines here
}
var msgHandlerObject = new Object();
msgHandlerObject.onMessage = myOnMessage;
msgHandlerObject.onError = myOnError;
msgHandlerObject.onDisclose = myOnDisclose;
function myOnDisclose(cURL,cDocumentURL)
{
return true;
}
function myOnError(error, aMessage)
{
app.alert(error);
}
this.hostContainer.messageHandler = msgHandlerObject;
Here's my problem: when I test this code and click the HREF Javascript link, I see "Hello from Javascript..." and "Hello from the PDF," so I know the postMessage event is working. However, I've tried every print command I know in the myOnMessage function, and nothing seems to work:
app.execMenuItem("Print"); // doesn't seem to work
this.print(); // doesn't seem to work, even when I pass in all the params, etc
I've also tried getting print params and passing those in, nothing seems to work. Am I missing something? Environement is Acrobat 9 Pro, browser's tested include Chrome 2.0, IE6. I've attached two files, the HTML bit and the PDF I'm trying to print. Any help is greatly appreciated!