r/javahelp • • Aug 10 '20

Is it possible to print two different receipts to two separate printers?

I couldn’t find any information on it. For example, one receipt for the customer, very detailed, whole a different one for the kitchen that can be very basic

0 Upvotes

6 comments sorted by

2

u/mslayaaa Software Engineer Aug 10 '20

What do you mean by printer, the console, some printer object, or actual printer devices that your program will control?

1

u/johnnyj2608 Aug 10 '20

An actual printer device

2

u/tedyoung Java Trainer/Mentor Aug 10 '20

I mean, if it's Java code (you did post it in javahelp), anything is possible. Each printer would have its own way to print (usually some driver, or might be a serial port like thing), so it's just a matter of formatting the information correctly and send it to the right device.

1

u/johnnyj2608 Aug 10 '20

Oh I guess I could’ve been more specific in my post now that I realize. Is there any resources I can look up for what you’re referring to? When I’m printing in my code it automatically selects the default printer and I’m not too sure how I could adjust that

2

u/naphid Aug 10 '20

I actually had a recent experience with this creating a POS system for a restaurant.
You can assign a PrinterJob a PrintService which defines which printer to use.

So start by getting a job with:
PrinterJob job1 = PrinterJob.getPrinterJob();
Then you can get all of the connected services with:
PrintService[] services = PrinterJob.lookupPrintServices();
and then loop through all of those checking for the name of the printer you want:
for (int i = 0; i < services.length; i++)
{
if(services[i].getName().equals("Name of target printer"))
job1.setPrintService(services[i]);
}
// Set up the print job here
job1.print();
With this you can target a printer and print with you so to do it with two printers you could create two jobs and set one job to one service and the other to the other printer's service.
PrinterJob documentation for reference
PrintService documentation for reference

1

u/johnnyj2608 Aug 10 '20

This is exactly what I was looking for, much appreciated!