1 2 Previous Next 17 Replies Latest reply on Apr 4, 2006 1:29 PM by 843798

    printing of pdf document from java application

    843797
      Hello,

      I want to print out a pdf document which I get from a server as a byte-stream. I don't want to display or preview it on the client but print it out right away. Is there a way to do this?

      Thank you very much!
      Matthias
        • 1. Re: printing of pdf document from java application
          843797
          http://www.lowagie.com/iText/index.html
          • 2. Re: printing of pdf document from java application
            843797
            So how does creating .pdf files tell someone who already has one how to send the bindary data of the file from the server directly to a print driver on a client system and have the driver render and print the data correctly?

            Something on the client system will have to read the binary data stream, render it as a .pdf, then create a spool file of some sort to pass off to the printer driver (probably in PostScript format.) Simplicity itself!

            (This means I don't know how, either, but I'd be interested in a solution. If you're wanting to do this from a web browser, you're probably out of luck, but if you have a Java client receiving the file from the server, there's probably a way, however tangled.)
            • 3. Re: printing of pdf document from java application
              843797
              iText does the conversion to pdf, but I already have a pdf dokument (as a Java stream object) that I simply want to display and print (without displaying) on the client. I also don't want to save the stream to a file first and then pass the file to acrobat.

              is there a way to do this? I tried the Acrobat java viewer which is very old and does not work at all.

              Matthias
              • 4. Re: printing of pdf document from java application
                843797
                I simply want to display and print (without displaying) on the client...
                is there a way to do this?
                Yes - first your applet must be trusted by the client PC (search these forums for details of signing your jar, and having the client trust the signer).

                Then have your applet write the stream to the disk as a file.

                Finally invoke the AcroRd32.exe as an external process, pass /p and the file name (so your whole argument to the command line would be "C:\whever\acrobat\is\installed\AcroRd32.exe /p c:\temp\streamToFile.pdf").

                You might have to play with the '/p' argument, I think it might have changed since I used it. Look at the adobe (www.adobe.com) site, and find the developer/sdk section. You'll have some research to do, but that's where I figured out how to do it...
                • 5. Re: printing of pdf document from java application
                  843797
                  i don't want to write a file but only to have a byte[]: is it possible?
                  • 6. Re: printing of pdf document from java application
                    843797
                    If u r already using iText to generate PDF documents and don't wan't to write the generated PDF content to some xxx.pdf u can write data in to memeory and then stream it to the client by using the following line of code

                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

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

                    These two line will make sure all the PDF contents stay in memory which could be later streamed to the client.

                    If in case u decide to write the content to a xxx.pdf file u can use the following line

                    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("xxx.pdf"));
                    • 7. Re: printing of pdf document from java application
                      843797
                      Coming back to printing the document with out user intervention u can achieve that too.

                      1. create a servlet which generates pdf content and store in the byte format
                      2. set the content type to the application.pdf
                      3. on the client side using combination of javascript and html to open a new IE/NN window which makes a call to this servlet which generates the document.
                      (i am assuming that u r working on web application)

                      If u can achieve this (I have done it, it is possible) your job is 90% done.
                      Here comes a bit of tweky work.
                      If one is using iText to generate PDF he/she can embed Adobe Javascript do some stuffs like printing the document to the default printer/ resizing the window size etc.
                      while generating the PDF content u can embed the following Javascript into the PDF content which will print the generated document without user intervention

                      To print to the default printer:

                      document.addJavaScript("this.print({bUI: false, bSilent: false});\r");

                      To prompt the end user for the printer:

                      document.addJavaScript("this.print({bUI: true, bSilent: false});\r");

                      If you target the servlet response to a hidden frame, Acrobat Reader won't display, unless
                      the user elects to not embed it in the browser.

                      Good Luck
                      • 8. Re: printing of pdf document from java application
                        843797
                        this old chestnut again - :)

                        Matthias - i suggest that you will have more luck printing using the InputStream flavours than with ByteArray. The same input sent via both flavours yielded a perfect result from the stream but printer confusion from the bytearray.

                        • 9. Re: printing of pdf document from java application
                          843797
                          BTW - Apache FOP will silently prepare the PDF that you require, use the Driver.RENDER_PDF class.

                          • 10. Re: printing of pdf document from java application
                            843798

                            Google Groups post on using the lpr command to do your PDF printing for you, apparently this works in UNIX and Windows 2000 or better:

                            http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=39BE23B0.AF6033B6%40wanadoo.fr&rnum=10&prev=/groups%3Fq%3Djava%2BPDF%2Bprinting%26btnG%3DGoogle%2BSearch%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8
                            • 11. Re: printing of pdf document from java application
                              843798
                              the document of itext doesn't have the addjavascript method.
                              • 12. Re: printing of pdf document from java application
                                843798
                                hi all,

                                i also faced the same problem as mention here, and i solved it as per your suggestions;
                                now comes to a point or situation in my web application:-

                                1. i want to save the generated pdf file in a file system, e.g. C:\test.pdf,
                                2. i also want to open it in browser to user and print it out without user intervention!

                                can it be achieved in anyway?
                                • 13. Re: printing of pdf document from java application
                                  843798
                                  you can acheive the first requirement using Apache FOP. Simply get a PDF/PCL bytestream out of FOP and save that to a file.
                                  • 14. Re: printing of pdf document from java application
                                    843798
                                    Hi Matthias,

                                    If you have got solution for this can u suggest me.
                                    I'm using following code

                                    {
                                              PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                                              DocFlavor flavor = javax.print.DocFlavor.INPUT_STREAM.AUTOSENSE;

                                              System.out.println("====================>"+flavor);

                                              PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
                                              PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
                                              PrintService service = ServiceUI.printDialog(null, 200, 200,printService, defaultService, flavor, pras);
                                              if (service != null)
                                              {
                                                   try
                                                   {

                                                        javax.print.attribute.standard.Copies obj=new javax.print.attribute.standard.Copies(1);
                                                        Attribute at=(Attribute)pras.get(obj.getClass());
                                                        int noOfCopies=1;
                                                        if(at!=null){
                                                             try{
                                                                  noOfCopies=Integer.parseInt(at.toString());

                                                             }catch(Exception err){noOfCopies=1;}
                                                        }

                                                        for(int i=0;i<noOfCopies;i++)
                                                        {

                                                             URL url = null;
                                                             try{
                                                                  url = new URL(urlToPDF);
                                                                  url.openConnection();
                                                                  fis = url.openStream();
                                                             }catch(Exception err){}

                                                             DocPrintJob job = service.createPrintJob();
                                                             job.addPrintJobListener(printjoblis);
                                                             DocAttributeSet das = new HashDocAttributeSet();
                                                             Doc doc = new SimpleDoc(fis, flavor, null);
                                                             pras.add(Sides.ONE_SIDED);
                                                             job.print(doc, pras);
                                                        }


                                                   }catch(Exception err){System.out.println("=========>"+err);}



                                              }
                                         }

                                    some times i'm getting garbage values in the printout.

                                    Thanks & Regards,
                                    Surya.
                                    1 2 Previous Next