fkleinko
fkleinko

Reputation: 121

Mocking a Printer

We have to build some software in Java that at the end prints some documents. Different documents should go to different trays of the printer. Because during development we don't have the same printer available as our customer, we are looking for a little piece of software that mocks a printer. We should be able to configure that mock, for example how many trays there are available.

Does anyone know such a tool for mac or windows?

Upvotes: 7

Views: 2919

Answers (3)

Abdul Fatah
Abdul Fatah

Reputation: 513

You can install a PDF print which can work as a virtual printer for your Java Application. Basically, what you to do is, install a freely available PDF printer and make your java application discover that print service and print whatever document to that service. I remember, I had the same situation when I did not have a printer, I used the code given below to interface my application with the 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);
}
}

Upvotes: 0

eis
eis

Reputation: 53462

You could create a dummy printer yourself on windows, without any special software.

In Windows 7:

  1. Control Panel
  2. Devices and Printers
  3. [Right click] Add a Printer
  4. Add a local printer
  5. Use an existing port (assuming it already exists, create a new one if it doesn't)
  6. File: (to print to a file), NUL: (to print nowhere) or CON: (to print to console)
  7. Select a printer you wish to emulate from the list of printers.

If you set it as default printer, it should be easy enough to use from java code.

Upvotes: 4

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298898

Write an abstraction layer which you implement once for your customer's "real" printer and once for a "virtual" printer. Write integration tests for the customer version, run those tests in your customer's environment. Code against the abstraction layer.

Upvotes: 4

Related Questions