Printing functionality while Vaadin app is running on a docker container

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.

I doubt that your idea is going to work. Server applications are not really meant to interact with the client’s printer

I suppose that even with docker this line does not get executed

System.err.println("Printer name found --- ” + ps.getName());

I think that this behavior is normal, since docker isolates the network from the host…

It is a printer that is on the network, I guess ?

How about the --network=host parameter for the docker container to access the host network ?

About the CUPS server ? wouldn’t it work for you ?

Hard to say … where is the app running? Where is the printer attached? If everything is running on your laptop then --network=host could help. If it is a network printer, that is.

Another thing you could try is to log on to the container and try to ping the address where the printer is.

If you are on a corporate network and the containter is running on some server, there might be firewalls blocking access to printer attached to your workstation.

You have to provide more info to get any further.

You have already been answered here about the CUPS as well.

Exactly, the line does not get executed. Maybe I missed something on the --network=host or something because I tried it and I was getting errors because I already had added the networks: - my-net in the Docker compose. Maybe I missed something on the documentation after a lot of trial, error and frustration. I’ll do a check again ASAP. Thank you for your feedback.

Yes, I just saw this too.

The app is running on docker in a Windows PC and plugged via USB. --network=host didn’t work for me and maybe I missed something on the documentation. Because I was getting errors while using it that I already have configured the networks: - my-net

I will check this out again and hoping that I did missed something and now it will work or else I’ll just abandon the idea of the Docker. Thank you for your feedback.

what model is your printer ?

Is it possible to connect it to the network ?

Or will it only go via USB ?

Even if it does not have a network/wifi connection etc, it might be possible to install a server on the host where it is connected.

for example

Create a small HTTP/REST service on the host that receives the print jobs, the Docker application sends the information to print to this service and the service on the Windows host handles the communication with the USB printer.

you always think, that to connect very diferents things many times via http, or with an endpoint you can connect 2 points no matter how different they are. :saluting_face:

1 Like