10 Replies Latest reply on Mar 17, 2010 12:19 PM by 807580

    Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)

      I've looked all around these forums, and all around the rest of the web, and from I can tell the best way to print a PDF document to a printer in Java is to use the javax.print.* (Java Print Service API stiff).

      I have managed to write (well, piece together) code that allows me to print images with the DocFlavor.INPUT_STREAM.JPEG/GIF, however when I try to print anything else (PDF, PS, TEXT, etc) the PrintServiceLookup returns a big fat ZERO for the number of printers that can handle these formats. I don't understand why JPEG and GIF would work, but not plain TEXT_PLAIN_US_ASCII which I'd assume is almost as basic as you can get...

      I am hoping for some insite, or some possible things to try. I am new to Java programming but as far as I can tell, the printer itself is being basically asked, "hey, can you print a document with this type and these settings (PrintRequestAttributeSet)?" My "printer" is a networked giant corperate laser printer and should be able to handle basically anything, I'd think...

      I've also tried a newer printer and get the same results - JPEG and GIF work perfectly, but nothing else does!
        • 1. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)
          here is the code:
          package coaImport;
          import java.io.*;
          import javax.print.*;
          import javax.print.attribute.*;
          import javax.print.attribute.standard.*;
          import javax.print.event.*;
          import SENAFileHandler.SENAFileHandler;
          public class PrintPDF {
               public static void main(String args[]) {
                    PrintPDF pdf = new PrintPDF();
               public PrintPDF() {
                    /* Construct the print request specification.
                     * The print data is Postscript which will be 
                      * supplied as a stream.  The media size 
                      * required is A4, and 2 copies are to be printed
                    System.out.println("Starting to create DocFlavor...");
                    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
                    System.out.println("DocFlavor set...\nSetting up AttributeSet...");
                    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                    System.out.println("Getting print service list...");
                    /* locate a print service that can handle it */
                    PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
                    System.out.println("PrintService pservices size = "+pservices.length);
                    if (pservices.length > 0) {
                         //for(int i=0; i<6; i++)
                         //     System.out.println("selected printer " + pservices.getName());
                         /* create a print job for the chosen service */
                         DocPrintJob pj = pservices[1].createPrintJob();
                         try {
                                   * Create a Doc object to hold the print data.
                                   * Since the data is postscript located in a disk file,
                                   * an input stream needs to be obtained
                                   * BasicDoc is a useful implementation that will if requested
                                   * close the stream when printing is completed.
                                   FileInputStream fis = new FileInputStream("C:\\Working Directory\\COA - Kevin\\share\\test.gif");
                                   System.out.println("File input went OK ");
                                   Doc doc = new SimpleDoc(fis, flavor, null);
                                   /* print the doc as specified */
                                   pj.print(doc, aset);
                                   * Do not explicitly call System.exit() when print returns.
                                   * Printing can be asynchronous so may be executing in a
                                   * separate thread.
                                   * If you want to explicitly exit the VM, use a print job
                                   * listener to be notified when it is safe to do so.

                         } catch (FileNotFoundException ie) {
                              System.out.println("SOMETHING WRONG");
                         } catch (PrintException e) {
                              System.out.println("SOMETHING WRONG");

          • 2. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)
            anybody have any great ideas?
            • 3. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)
              new day! anybody new with any new ideas? <3
              • 4. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)

                I've got the same problem.

                For me the DocFlavor.INPUT_STREAM.PDF works just fine under OSX, and it prints fabulous quality, but today when I tried it under Win XP java said: invalid flavor.

                I don't know why, but it works if I try to print PNG files. Anyway I don't have the same printer, that I had yesterday, maybe that is the problem...

                Anybody knows the solution??

                thnx, giotto
                • 5. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)

                  I've tested this problem under XP with the same problem. It is not the printer's fault.

                  Under OSX the DocFlavor.INPUT_STREAM.PDF works fine, but under windows it does not!

                  I don't know why, or how to solve this problem. Does anyone have any good idea?

                  • 6. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)

                    I've searched the net and the API, and there is no good solution for this DocFlavor problem under windows.

                    Before printing the PDF file has to be encoded.
                    Under XP this is a possible way to print PDF, even if it is not too nice:
                         public void printPDF(String s){
                                 Runtime.getRuntime().exec("cmd.exe /C start acrord32 /P /h" + s);
                            } catch (Throwable t){
                    It uses the adobe acrobat reader to print. The /P switch calls the printer in acrobat, and with the /h switch you can print in silence.

                    I still don't know why but under OSX DocFlavor.INPUT_STREAM.PDF works fine.
                    • 7. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)
                      Hi, I've only just joined up to this forum to look for answers to PDF printing... the main reason I'm looking is that your method described here using acrord32 is illegal. :-(

                      We used to print that way too, but if you check out Adobe's EULA, section 2.2.1, you aint allowed to do any programmatic printing anymore... http://www.adobe.com/products/acrobat/acrreula.html

                      I've since implemented PDF printing using ghostscript and ghostview, but it has it's problems, which is why I've come here, looking for more answers. To be honest, I haven't tried the javax.print, and it looks like I shouldn't either, given that we're on Win XP. I'll keep searching for now.
                      • 8. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)
                        Unless you printer support PDF natively (HP laserjet 9050) you will have to use a library like JPedal or jPDFPrint. If you project isn't Open Source, it is a bit expensive.
                        Java Print Service support PostScript if it is an option for you.
                        • 9. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)
                          I've tried the following code. It will print a text file. BUT... I need it to print with Landscape.
                          PLEASE HELP.........

                          PrintService service = PrintServiceLookup.lookupDefaultPrintService();

                          InputStream in = new FileInputStream("test.dat");

                          DocPrintJob job = service.createPrintJob();
                          DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

                          PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

                          HashDocAttributeSet hdas = new HashDocAttributeSet();

                          Doc doc = new SimpleDoc(in, flavor, hdas);

                          //Want to make sure we have Landscape
                          DocAttributeSet das = doc.getAttributes();
                          Attribute[] dasa = das.toArray();
                          for(int i = 0; i < dasa.length; i++)
                          System.out.println("Attribute[" + i + "] = " +
                          dasa.getName() + " -- " + dasa.toString());

                          //print the document
                          job.print(doc, aset);
                          • 10. Re: Problems with DocFlavor.INPUT_STREAM.PDF (printing PDFs)
                            did you got solution for printing PDF files ?