I need to print from my executed program as a Windows service

1

I have a program created in Java that gets a PDF and prints it. To print it use ghostscript:

public static void print(String nomImpresora, String rutaNombreArchivo, int cantVia) throws Exception {
    String cmd;
    cmd = "gswin64c -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + cantVia + " -sDEVICE=mswinpr2 -dNoCancel -sOutputFile=\"\\spool\" + nomImpresora + "\" " + rutaNombreArchivo;
    Process p = null;
    try {
        p = Runtime.getRuntime().exec(cmd);
        Log.addLog(Log.tipoMensaje.InfoSis, "Se envio a Imprimir a la impresora: " + Conf.o().getNomImpresora());
        Log.addLog(Log.tipoMensaje.InfoSis, "Imprime eFacturaPrint: S, Formato: " + Conf.o().getForamtoImpresion() + ", " + Conf.o().getNumVias() + " vias");
    } catch (IOException e) {
        Log.addLog(Log.tipoMensaje.ErrorGenerico, "Error al intentar imprimir con gs: " + e.toString());
    } finally {
        if (p != null) {
            p.destroy();
        }
    }
}

From Netbeans, or running the program from console works perfectly.

What happens is that this program should run in the background at the beginning of the system. So first create a bat that runs the program and set it in the task scheduler. Because the task scheduler does not stop it or start I have investigated that it is best to wrap it, which is why I used JSmooth. I create the service, I install it, it works, with the exception that it does not print. For better of the case I do not capture exceptions.

I tried to send directly to the printer but being a PDF my printer does not interpret it and prints any text.

I appreciate your interest.

    
asked by Juan 13.07.2018 в 17:20
source

1 answer

0

Finally, doing a lot of research, I found another solution to print. I use the Apache PDDocument library. An essential requirement is to eliminate all the outputs and make the log in text file. I have also separated the program into two. A module that manipulates the data and creates the pdf. The other only prints by invoking System.Runtime. This second module can not have outputs on the screen either.

On the internet you will find a lot about not doing windows services that print, since the windows API specifies that it is not advisable. In my experience, with jdk 1.7, PDDocument and JSmooth I have achieved it after many tests.

    
answered by 19.07.2018 в 20:34