I am creating 2 containers with the docker-compose.yml, first container is a MariaDB and the second is my Vaadin app. These two containers are communicating with via a custom network
networks:
- my-net
In the app there is a functionality that prints a barcode.png on a label printer with specific dimensions. This function works perfectly when it’s not running in the docker container, of course because the printer can be detected.
So basically I have a layout and generate a button to trigger the printing
HorizontalLayout actionButtonsLayout = new HorizontalLayout(
createNewDialog(ext),
createNewExt(clService, ext),
extBtn,
BarcodeGenerator.createBarcodeButton(ext.getSerialNumber())); //<---THIS BUTTON
Inside the BarcodeGenerator class I have multiple methods but the one with the problem is the below:
public static void printBarcode(BufferedImage sticker) {
try {
PrinterJob printerJob = PrinterJob.getPrinterJob();
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
PrintService ql700Printer = null;
for (PrintService ps : printServices) {
System.err.println("Printer name found --- " + ps.getName());
if (ps.getName().contains("Brother QL-700")) {
ql700Printer = ps;
break;
}
}
if (ql700Printer == null) {
System.err.println("Error while printing. No printer found.");
throw new IllegalStateException("Brother QL-700 not found.");
}
PageFormat pageFormat = printerJob.defaultPage();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Paper paper = pageFormat.getPaper();
// Here I do label configuration
// .
// .
// .
//
try {
printerJob.print();
System.out.println("✅ Print job sent successfully.");
} catch (PrinterException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error while printing");
}
}
Providing the above method in order to give context on how I do the printing in the app.
I’ve searched the forums for an answer to my problem for the last 2 days but everything that I tried seems to not be working for my situation, trigger the printing from the app.
I know that this question is not exactly the problem of the Vaadin app itself and I am not sure if posting it here is okay, but I thought maybe someone have tried something similar and have an answer on how to detect that specific printer and print the label when the app runs in a container?
Any guidance will be appreciated.
Thank you.