Printer bullying

We need to create some Java software that prints some documents at the end. Different documents must be delivered to different printer trays. Because during development we do not have the same printer as our client, we are looking for a small piece of software mocking the printer. We should be able to customize this layout, for example, how many trays are available.

Does anyone know such a tool for mac or windows?

+7
source share
3 answers

Write an abstraction layer that you once implement for your "real" printer, and once for a "virtual" printer. Write integration tests for the client version, run these tests in the client environment. Code against an abstraction layer.

+4
source

You can create a dummy printer yourself on windows without special software.

In Windows 7:

  • Control Panel
  • Devices and Printers
  • [Right click] Add Printer
  • Add local printer
  • Use an existing port (if it already exists, create a new one if it does not work)
  • File: (to print to a file), NUL: (to print anywhere) or CON: (to print to the console)
  • Select the printer you want to emulate from the list of printers.

If you install it as the default printer, it should be fairly simple to use from java code.

+3
source

You can install PDF printing, which can act as a virtual printer for your Java application. Basically what you need to do is install a freely available PDF printer and make your Java application discover this print service and print any document for this service. I remember, I had the same situation when I did not have a printer, I used the code below to interface my application with a virtual printer.

public class HelloWorldPrinter implements Printable, ActionListener { public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { /* We have only one page, and 'page' is zero-based */ return NO_SUCH_PAGE; } /* User (0,0) is typically outside the imageable area, so we must * translate by the X and Y values in the PageFormat to avoid clipping */ Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); /* Now we perform our rendering */ g.drawString("Hello world!", 100, 100); /* tell the caller that this page is part of the printed document */ return PAGE_EXISTS; } public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); PrintService[] printServices = PrinterJob.lookupPrintServices(); try { job.setPrintService(printServices[0]); job.print(); } catch (PrinterException ex) { Logger.getLogger(HelloWorldPrinter.class.getName()).log(Level.SEVERE, null, ex); } } public static void main(String args[]) { UIManager.put("swing.boldMetal", Boolean.FALSE); JFrame f = new JFrame("Hello World Printer"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); JButton printButton = new JButton("Print Hello World"); printButton.addActionListener(new HelloWorldPrinter()); f.add("Center", printButton); f.pack(); f.setVisible(true); } } 
0
source

All Articles