Quantcast
Channel: Adobe Community : Popular Discussions - JavaScript
Viewing all articles
Browse latest Browse all 12130

this.print(pp) with interactive type silent or automatic does not work after upgrade acrobat reader 9 to acrobat reader 11

$
0
0

After an upgrade of acrobat reader 9 to acrobat reader 11 the automatic printing of a pdf. The pdf is opened, but the print does not happen. With acrobat reader 9 it works. But with acrobat reader 11 the printing does not happen.

I discovered when you specify pp.constants.interactionLevel.full it works on acrobat reader 11. But when you specify pp.constants.interactionLevel.silent or pp.constants.interactionLevel.automatic it does not work on acrobat reader 11. But with the full option we have a dialog  print box

we do not want.

 

 

In our jsp we load a pdf document and create a message handler to detect the print events of a pdf document that is in an <object> tag.

In the pdf document we add

 

package be.post.lpo.util;

 

import org.apache.commons.lang.StringEscapeUtils;

import org.apache.commons.lang.StringUtils;

public class AcrobatJavascriptUtil {  

 

    public String getPostMessageToHostContainer(String messageName, String messageBody){

        return "var aMessage=['"+messageName+ "', '"+ messageBody+"'];this.hostContainer.postMessage(aMessage);";

    }

    public String getAutoPrintJavaScript(String interactiveType, String printerName,String duplexType) {    

        String interactiveTypeCommand = "";

        if (StringUtils.isNotBlank(interactiveType)){

            interactiveTypeCommand = "pp.interactive = " + interactiveType + ";";

        }

        String duplexTypeCommand = "";

        if (StringUtils.isNotBlank(duplexType)){

            duplexTypeCommand = "pp.DuplexType = " + duplexType + ";";

        }

    

        return "" + // //

                "var pp = this.getPrintParams();" + // //

                // Nointeraction at all dialog, progress, cancel) //

                interactiveTypeCommand + //

                // Always print to a printer (not to a file) //

                "pp.printerName = \"" + StringEscapeUtils.escapeJavaScript(printerName) + "\";" + //

                // Never print to a file. //

                "pp.fileName = \"\";" + //

                // Print images using 600 DPI. This option MUST be set or some barcodes cannot //

                // be scanned anymore. //

                "pp.bitmapDPI = 600;" + //

                // Do not perform any page scaling //

                "pp.pageHandling = pp.constants.handling.none;" + //

                // Always print all pages //

                "pp.pageSubset = pp.constants.subsets.all;" + //

                // Do not autocenter //

                "pp.flags |= pp.constants.flagValues.suppressCenter;" + //

                // Do not autorotate //

                "pp.flags |= pp.constants.flagValues.suppressRotate;" + //

                // Disable setPageSize i.e. do not choose paper tray by PDF page size //

                "pp.flags &= ~pp.constants.flagValues.setPageSize;" + //

                // Emit the document contents. Document comments are not printed //

                "pp.printContent = pp.constants.printContents.doc;" + //

                // printing duplex mode to simplex, duplex long edge, or duplex short edge feed //

                duplexTypeCommand +

                // Print pages in the normal order. //

                "pp.reversePages = false;" + //

                // Do the actual printing //

                "this.print(pp);";

    }

}

 

 

 

 

snippets for java code that adds

 

package be.post.lpo.util;

 

import org.apache.commons.lang.StringUtils;

 

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.pdf.PdfAction;

import com.lowagie.text.pdf.PdfDestination;

import com.lowagie.text.pdf.PdfImportedPage;

import com.lowagie.text.pdf.PdfName;

import com.lowagie.text.pdf.PdfReader;

import com.lowagie.text.pdf.PdfWriter;

 

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

 

 

public class PdfMergerUtil{

 

    private static final PdfName DID_PRINT = PdfName.DP;

    private static final PdfName WILL_PRINT = PdfName.WP;

 

    

    private List<PdfActionJavaScriptHolder> actionJavaScripts = new ArrayList<PdfActionJavaScriptHolder>();

 

    private class PdfActionJavaScriptHolder{

    

        private final PdfName actionType;

        private final String javaScript;

    

        public PdfActionJavaScriptHolder(PdfName actionType, String javaScript) {

            super();

            this.actionType = actionType;

            this.javaScript = javaScript;

        }

    

        public PdfName getActionType(){

            return this.actionType;

        }

    

        public String getJavaScript(){

            return this.javaScript;

        }

    

    }

 

   

    public void writePdfs(OutputStream outputStream, List<InputStream> documents, String documentLevelJavaScript) throws Exception {

        Document document = new Document();

        try {          

          // Create a writer for the outputstream

          PdfWriter writer = PdfWriter.getInstance(document, outputStream);

          document.open();      

      

          // Create Readers for the pdfs.

          Iterator<PdfReader> iteratorPDFReader = getPdfReaders(documents.iterator());

                        

          writePdfReaders(document, writer, iteratorPDFReader);

          if (StringUtils.isNotBlank(documentLevelJavaScript)){

              writer.addJavaScript(documentLevelJavaScript);

          }

          addAdditionalActions(writer);

      

          outputStream.flush();      

      

        } catch (Exception e) {

            e.printStackTrace();

            throw e;

        } finally {

            if (document.isOpen()){

                document.close();

            }

            try {

                if (outputStream != null){

                    outputStream.close();

                }

            } catch (IOException ioe) {

                ioe.printStackTrace();

                throw ioe;

            }

        }

    

    }

 

    public void addAdditionalDidPrintAction(String javaScript){

        actionJavaScripts.add(new PdfActionJavaScriptHolder(DID_PRINT, javaScript));   

    }

 

    public void addAdditionalWillPrintAction(String javaScript){

        actionJavaScripts.add(new PdfActionJavaScriptHolder(WILL_PRINT, javaScript));   

    }

 

 

 

    private void writePdfReaders(Document document, PdfWriter writer,

            Iterator<PdfReader> iteratorPDFReader) {

        int pageOfCurrentReaderPDF = 0;      

          // Loop through the PDF files and add to the output.

          while (iteratorPDFReader.hasNext()) {

            PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.

            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {

              document.newPage();

              pageOfCurrentReaderPDF++;          

              PdfImportedPage page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);

              writer.getDirectContent().addTemplate(page, 0, 0);          

            }

            pageOfCurrentReaderPDF = 0;

          }

    }

 

    private void addAdditionalActions(PdfWriter writer) throws DocumentException{

        if (actionJavaScripts.size() != 0 ){

            PdfAction action = PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.FIT), writer);

            writer.setOpenAction(action);

            for (PdfActionJavaScriptHolder pdfAction : actionJavaScripts ){

                if (StringUtils.isNotBlank(pdfAction.getJavaScript())){

                    action = PdfAction.javaScript(pdfAction.getJavaScript(), writer);

                    writer.setAdditionalAction(pdfAction.getActionType(), action);

                }

            }

        }

    }

 

 

 

 

    private Iterator<PdfReader> getPdfReaders(Iterator<InputStream> iteratorPDFs) throws IOException {

        List<PdfReader> readers = new ArrayList<PdfReader>();

          while (iteratorPDFs.hasNext()) {

            InputStream pdf = iteratorPDFs.next();

            PdfReader pdfReader = new PdfReader(pdf);

            readers.add(pdfReader);        

          }

        return readers.iterator();

    }

 

 

 

}

 

 

 

 

 

 

JSP code

<script type="text/javascript" src="<bean:message key="scripts.js.internal.jquery" bundle="app"/>"></script>

    <script language="javascript">

    function goToDidPrintUrl(){

        var url = "<%=didPrintUrl%>";

        window.location.assign(url);

    }

    function createMessageHandler() {

        var PDFObject = document.getElementById("myPdf");

        PDFObject.messageHandler = {

            onMessage: function(msg) {

                 if (msg[0] == "WILL_PRINT"){      

                    $("#printingTransitFeedBackMessage").text('<%=willPrintMessage%>');                   

                 }             

                 if(msg[0] == "DID_PRINT"){

                    $("#printingTransitFeedBackMessage").text('<%=didPrintMessage%>');               

                    setTimeout('goToDidPrintUrl()',4000);

                 }                            

            },

            onError: function(error, msg) {

                alert(error.message);

            }

        }

    }

    </script>

</head>

<body onLoad="createMessageHandler();">

<div id="printingTransitFeedbackArea">

  <div class="info" id="printingTransitFeedBackMessage"><%=documentOpenMessage%></div>

</div>

<object id="myPdf" type="application/pdf" width="100%" height="100%"  data="<%=toBePrintedUrl%>">

</object>

</body>



Viewing all articles
Browse latest Browse all 12130

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>