I need direct-to-printer functionality for my website, with the ability to distinguish a physical printer from a virtual printer (file).
Coupons.com has this functio
OK here is what I've found so far (this is not an exhaustive test by any means, but it has been a fun problem to try and tackle). it seems that there might be some help by looking at how the validatePage() method in the PrinterJob class works. It seems that if a printer job is virtual than any attempt to set a page's ImageableArea will always return a value exactly equal to the default pages ImageableArea while an attempt to to the same to a real printer will return slightly smaller values (to account for the edges of the paper being held by the printer mechanics. What helps for this problem is that if you just ask a printer for it's default characteristics before calling validate you get an optimistic result, and if you compare this to a validated response you can do a simple if test. I've written some code for this that seems to work with the image printers and real printers I have on my desktop (again this isn't exhaustive, but it could work as a starting point)
import java.awt.print.*;
import javax.print.PrintService;
import javax.print.attribute.Attribute;
public class DetectFilePrinter {
public static void main(String[] args) {
PrinterJob job = PrinterJob.getPrinterJob();
PrintService printer = job.getPrintService();
System.out.println("Printer Name:"+printer.getName());
System.out.println(printer.toString());
PageFormat page = job.defaultPage();
double default_width = page.getWidth();
double default_height = page.getHeight();
Paper paper = new Paper();
paper.setImageableArea(0, 0, Double.MAX_VALUE, Double.MAX_VALUE);
page.setPaper(paper);
PageFormat fixed_page = job.validatePage(page);
double fixed_width = fixed_page.getImageableWidth();
double fixed_height = fixed_page.getImageableHeight();
//So far all of my tested "image printers" return the same
//height and width after calling validatePage()
if(default_height == fixed_height && default_width == fixed_width) {
System.out.println("This looks like a \"image printer\"");
} else {
System.out.println("This looks like a \"real printer\"");
}
}
}