0

So i have an application that sends tasks to a printer. Everything is working fine if the printer i'm sending tasks to is set to "default printer", the moment i change the default printer and try sending tasks again to this same printer which is already not the default one i get the following picture: through java it finds the correct printer, sends a task and in the printing queue the statuses change as follows - Spooling, printing and sent to Printer. After a few seconds sent to Printer status disappears and nothing happens, no errors, nothing. Watching my code i can't really see any problems. It finds everything correctly and i'm not really sure where to look for a solution and which code snippet to provide here in order to make the picture more clear.

It certainly gets through this point correctly:

private void sendDataToPrinter(PrinterContent printerContent) {
    //Get available printers
    PrintService[] printServices = Constants.PRINT_SERVICES;

    //Look for right printer and print data
    for (int i = 0;  i < printServices.length; i++) {

        if (printServices[i].getName().equals(printerContent.getPrinterName())) {
            print(printServices[i], printerContent);
            break;
        }
    }
}

This is the print method:

private void print(PrintService printer, PrinterContent printerContent) {

    Document htmlDocument = ConverterUtil.convertStringToHtmlDocument(printerContent.getPrintContent());
    PDDocument pdfDocument = ConverterUtil.convertHtmlToPdf(htmlDocument);
    PrinterJob printJob = createPDFPrinterJob(pdfDocument);

    try {
        printJob.setPrintService(printer);
        printJob.print();
    }
    catch(PrinterException ex) {
        ex.printStackTrace();
    }
}

And the createPDFPrinterJob method:

private PrinterJob createPDFPrinterJob(PDDocument pdfDocument) {

    PrinterJob printJob = PrinterJob.getPrinterJob();

    PageFormat pageFormat = printJob.defaultPage();
    pageFormat.setOrientation(PageFormat.PORTRAIT);

    pageFormat.setPaper(PrinterJob.getPrinterJob().defaultPage().getPaper());

    try {
        printJob.setPrintable(new PDPageable(pdfDocument), pageFormat);
    }
    catch(PrinterException ex) {
        ex.printStackTrace();
    }

    return printJob;
}
R.Ro
  • 449
  • 1
  • 7
  • 15
  • _Watching my code i can't really see any problems_ How about sharing your code with us? Please read about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Clijsters Nov 28 '17 at 12:34
  • @Clijsters didn't want to provide a lot of code since i don't really know where the problem is. But i'll try , give me a moment. – R.Ro Nov 28 '17 at 12:36

0 Answers0