1

I developing an app with Java 8 using Spring. The user story is that the client should select many document (PDF) to be printed and then press a button and print all of them to the default client machine printer (Ctrl+P function from browser won't accomplish this requirement). But, I'm facing problem when trying to print on the CLIENT MACHINE printer using Java Service Printer API. Based on the documentation, JPS API should also run in client (as I understand), but I cannot make this work:

Java Service Printer API Documentation

I'm really stuck. I've seen some other options like using an Applet, but this option is being blocked in the lasted version of browsers (at least in Chrome).

If there are some others alternative, please let me know how to implement them:

Here is the code I'm using:

@GetMapping(value = Constantes.IMPRIMIR_FACTURA)
@CrossOrigin(origins = "*")
public void imprimirFacturas(@PathVariable("fechaFacturacion") String fechaFacturacion,
                             @RequestParam("polizas") String polizas,
                             @RequestParam("ordenarPor") String ordenarPor,
                             @RequestParam("directorId") String directorId,
                             @RequestParam("gerenteId") String gerenteId,
                             @RequestParam("intermediarioId") String intermediarioId,
                             @RequestParam("frecuenciaDePagoId") String frecuenciaDePagoId,
                             @RequestParam(value = "imprimePortada", defaultValue = "false") String imprimePortada,
                             @RequestParam(value = "imprimeDetalle", defaultValue = "false") String imprimeDetalle,
                             @RequestParam(value = "imprimeMovimientos", defaultValue = "false") String imprimeMovimientos,
                             HttpServletResponse response) throws IOException {
    String[] split = polizas.split(",");
    String ordenByDelReporte = determinarOrden(ordenarPor);

    try {
        List<String> polizasString = Arrays.asList(split);
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();

        boolean efectuarImpresion = false;
        if (Objects.isNull(printService))
            throw new PrinterAbortException("No existe impresora por defecto.");
        for (String numeroPoliza : polizasString) {

            FacturaDto facturaDto = new FacturaDto()
                    .imprimePortada(imprimePortada).imprimeDetalle(imprimeDetalle).imprimeMovimientos(imprimeMovimientos)
                    .ordenarPor(ordenByDelReporte).conFechaFacturacion(fechaFacturacion)
                    .numeroPoliza(numeroPoliza);

            facturaDto.setDirectorId(directorId);
            facturaDto.setGerenteId(gerenteId);
            facturaDto.setIntermediarioId(intermediarioId);
            facturaDto.setFrecuenciaDePagoId(frecuenciaDePagoId);

            byte[] objPDF = generarPorPdf(facturaDto);

            if (objPDF.length == Constantes.CERO)
                continue;
            efectuarImpresion = true;

            Doc pdf = new SimpleDoc(objPDF, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
            printService.createPrintJob().print(pdf, new HashPrintRequestAttributeSet());
        }
        if (!efectuarImpresion)
            response.setStatus(333);
    } catch (Exception e) {
        LOGGER.error(Constantes.ERROR, e);
        response.sendError(500, e.getMessage());
    }
}

This works but only if the printer is installed and selected as a default printer on the server.

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
Jeremy410
  • 41
  • 1
  • 7
  • See https://stackoverflow.com/questions/7074070/how-to-send-a-pdf-file-directly-to-the-printer-using-javascript – rgrebski Feb 07 '18 at 19:29
  • Is your client is a native web browser? If it is - you cannot use Java API on the client side... – Shay Feb 07 '18 at 19:34
  • Yes @Shay it is a web browser. Don't you know any other possible solutions then? – Jeremy410 Feb 07 '18 at 19:39
  • @rgrebski that does work for multiple PDF documents. Also, my PDF are in bytes, – Jeremy410 Feb 07 '18 at 19:41
  • Since your'e client is running in a container (a web browser), when you call the print function you are calling a service exposed by the container, and you have no control over it's implementation (it depends on both the web browser and OS). I believe that even if you will find a way to do it, it is not guaranteed to work cross-web-browsers. – Shay Feb 07 '18 at 19:43
  • There might be some browser specific option (IE ActiveX if that is even still around), but generally this is not possible. The best you can do is pop up the Print Dialg box. Change the requirement. If there is push back ask for an example web site that supports this and works in all browsers. – Andrew S Feb 07 '18 at 19:49

0 Answers0