1 Reply Latest reply on Oct 27, 2009 8:00 AM by Patrick Leckey

    Printing a PDF via Acrobat Javascript

    psalvo0001

      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!

        • 1. Re: Printing a PDF via Acrobat Javascript
          Patrick Leckey Level 3

          You have to be careful when using the ActiveX control to communicate with system functions like printing.  Because it can be embedded in anything that supports ActiveX, some functionality is limited from being called from JavaScript as it may interfere with the host application.

           

          Check out the IAC API Reference (also included with the SDK) and the methods under AxAcroPDFLib.AxAcroPDF.  There are some print functions in there (like PrintAll) that you can call on the ActiveX control directly, as in from your doPrint() method in your HTML page without using the hostContainer.  You would basically just call:

           

          pdfObject = document.getElementById("PDFObj");

          pdfObject.PrintAll();

           

          These methods were designed specifically to work with the ActiveX control when embedded in a host application, and can be used to print a PDF embedded in a web page.