3

I've been trying to determine whether a printer is capable of printing a Postscript (.ps) file. Using the following method, I've attempted to determine whether a printer can print the Postscript with the following code:

private void findPrinters( DocFlavor flav ) {
  PrintService[] services = PrintServiceLookup.lookupPrintServices( flav, null );

  System.out.println( flav );
  if ( services != null && services.length != 0 ) {
    System.out.println( "SUPPORTED:" );
    for ( PrintService service : services ) {
      System.out.println( service );
    }
  } else {
    System.out.println( "NOT SUPPORTED" );
  }
}

I've passed it DocFlavor.INPUT_STREAM.POSTSCRIPT or DocFlavor.BYTE_ARRAY.POSTSCRIPT and it would return Win32 Printer : HP LaserJet 2300L PS and Win32 Printer : CutePDF Writer (both of which are "virtual" printers - the former used to created PDF and the latter used to create Postscript). After installing "HP Universal Print Driver for Windows PostScript", Win32 Printer : HP Universal Printing PS shows up as Postscript capable. However, I'm not certain if this driver is being used or not therefore I interpret this to be that none of the other attached printers can print a Postscript file.

Then I tried to feed a Postscript file to a couple of attached printers with the following sample code:

FileInputStream in = null;
PrintService prnSvc = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = prnSvc.createPrintJob();
Doc doc;

in = new FileInputStream( "some.ps" );

if ( prnSvc.isDocFlavorSupported( DocFlavor.INPUT_STREAM.POSTSCRIPT ) ) {
  /* USE THIS FOR A PS-capable printer */
  doc = new SimpleDoc( in, DocFlavor.INPUT_STREAM.POSTSCRIPT, null );
} else {
  /* USE THIS FOR OTHER PRINTER */
  doc = new SimpleDoc( in, DocFlavor.INPUT_STREAM.AUTOSENSE, null );
}

printJob.print( doc, null );

It would print correctly on the Postscript capable printers as well as one of the network printers (a HP LaserJet P4014N) that Java says doesn't have Postscript support. On my receipt printer, it would print the Postscript file as if it were just text. It has been suggested to me that the network printer isn't responding correctly to Java (generic drivers, etc.) and that it could actually support Postscript. I've looked at documentation but couldn't determine whether LaserJet P4014N supports Postscript or not. I did find another printer (HP Laserjet 2055dn) and connected via USB to my computer and Java still reports that it doesn't support Postscript despite being able to print it with the above method.

Maybe I'm doing something wrong? If not, how do I correctly detect whether a printer supports Postscript? My ultimate goal is to determine whether a printer has Postscript support or not, if it doesn't convert the Postscript to JPG (loss in resolution, but the source does have images) and print that. If you have any suggestions for printing a Postscript file to a non-Postscript-capable printer, I'd love to hear it! Please let me know if you need additional information.

I found some sample Postscript files here that you could use.

nevets1219
  • 7,692
  • 4
  • 32
  • 47

2 Answers2

2

(this is too long for a comment)

My ultimate goal is to determine whether a printer has Postscript support or not

If you want to determine it programmatically then the following may not be of great help but...

I've looked at documentation but couldn't determine whether LaserJet P4014N supports Postscript or not.

When a printer natively speaks PostScript and you've got the printer's IP you do not need drivers: you can simply send the PostScript file to the printer and the printer is going to print it.

For example here on my home office I've got a HP printer with PostScript support on local IP 10.0.0.44 and I can simply print .ps files to that printer by doing, from the command line:

cat test.ps | netcat 10.0.0.44  9100

Now I know it's not an answer to your question (hence the comment too long for a comment) but if you do that (or send the file directly from Java, without using any driver) and the printer prints the PostScript file correctly, then you can be sure that your printer does indeed speaks PostScript natively (which may help you to troubleshoot your Java / PostScript support thing).

TacticalCoder
  • 6,275
  • 3
  • 31
  • 39
  • btw don't ask me why I do *cat* then *netcat*: I've done it like this since so long that it became an habit. – TacticalCoder Jan 18 '12 at 19:08
  • Also note that the model number is not always enough to determine if you have native PostScript or not. To give an example, you have a lot of good old (amazing) LaserJet 4+ that have received a module allowing them to natively render PostScript although, by default, the 4+ wasn't supporting PostScript (contrarily to the 4M+: they all supported PostScript from the start). – TacticalCoder Jan 18 '12 at 19:12
  • This seems like an easier way to test but I do need to programatically know whether a printer supports Postscript since I can't expect the end-user to do the same thing. – nevets1219 Jan 18 '12 at 19:13
  • @nevets129: yeah I guessed so, which is why I mentioned it wasn't an answer : ( At least it may help you know for sure if the printer you mentioned has native PostScript support or not : ) – TacticalCoder Jan 18 '12 at 19:35
0

I'm not familiar with the Java SE printing environment. However, just looking at the PrintServiceLookup API docs, I wonder why you are using PrintServiceLookup.lookupDefaultPrintService(). This gives you the PrintService that the environment prefers. It seems that PrintServiceLookup.lookupPrintServices(DocFlavor flavor, AttributeSet attributes) would let you specify the PrintService which you prefer.

The DocFlavor documentation says you can specify a MIMEtype of "application/postscript" to direct the system to give you a PrintService which accepts PostScript language data.

In fact, there appears to be a predefined DocFlavour.BYTE_ARRAY POSTSCRIPT class which might be a useful parameter for you.

Jim DeLaHunt
  • 10,960
  • 3
  • 45
  • 74
  • The second block of code which does the actual printing uses the default printer which I manually change to whichever printer I'm testing. The first block of code is what is telling me if a printer supports Postscript or not. – nevets1219 Jan 18 '12 at 19:10