How can I print JasperPrint directly to printer?

15,795

Solution 1

I solved it as below, hopefully it helps someone else.

public class PrintApp  {

    public static void print() {
            JasperPrint jasperPrint = getJasperPrint();
            String selectedPrinter = AllPrinter.getDepartmentPrinter("Admin");

            PrinterJob printerJob = PrinterJob.getPrinterJob();
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            PrintService selectedService = null;

            if(services.length != 0 || services != null)
            {
                for(PrintService service : services){
                    String existingPrinter = service.getName().toLowerCase();
                    if(existingPrinter.equals(selectedPrinter))
                    {
                        selectedService = service;
                        break;
                    }
                }

                if(selectedService != null)
                {
                    printerJob.setPrintService(selectedService);
                    boolean printSucceed = JasperPrintManager.printReport(mainPrint, false);
                }
    }

    private static JasperPrint getJasperPrint() {
            return JasperPrinterCreator.getJasperprint();
    }
}

Solution 2

private void PrintReportToPrinter(JasperPrint jp, String impresora) {
    try {
        // TODO Auto-generated method stub
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        // printRequestAttributeSet.add(MediaSizeName.ISO_A4); //setting page size
        printRequestAttributeSet.add(new Copies(1));

        PrinterName printerName = new PrinterName(impresora, null); //gets printer

        PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
        printServiceAttributeSet.add(printerName);

        JRPrintServiceExporter exporter = new JRPrintServiceExporter();

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
        exporter.exportReport();
    } catch (JRException ex) {
         JOptionPane.showMessageDialog(null,"Cancelo Impresion");
    }
}
Share:
15,795
michdraft
Author by

michdraft

A java developer.

Updated on June 04, 2022

Comments

  • michdraft
    michdraft almost 2 years

    I use Java to create report with JasperReports. What i want to do is that user be able to print directly, without print dialog.
    I create JasperPrint and I know name and model of my printer.

    I have also looked in the sample here but could not figure out how.
    I use Java 1.7 and latest JasperReports library.

    Does anyone know how to do it?

    public class PrintApp  {
    
        public static void print() {
            JasperPrint jasperPrint = getJasperPrint();
                String printername = AllPrinter.getDepartmentPrinter("Admin");
                 // where should i introduce my printer name to jasperreports?
                JasperPrintManager.printReport(jasperPrint, false);
        }
    
        private static JasperPrint getJasperPrint() {
                return JasperPrinterCreator.getJasperprint();
        }
    }