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.