0

I'm developing a web application using jsf, and I have create a jasper report file using iReport.

I try to print this file automatically (when I click a button the method send the file directly to the default printer selected in the system).

My problem is that the printing is in the server side (it means that if I tray to print from another user connected to the server,the file is printed in the printer of the server.

My question is: is there a method to print those files in the client printer?

this is my code to print server side but it's not work in the client side, means that it's printed in the server printer,

import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PrinterName;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;



    private void PrintReportToPrinter(JasperPrint jp) throws JRException {
        // TODO Auto-generated method stub
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        // printRequestAttributeSet.add(MediaSizeName.ISO_A4); //setting page size
        printRequestAttributeSet.add(new Copies(1));
        System.out.println("01-*********************");
        PrintService[] services = PrinterJob.lookupPrintServices();
        for (PrintService prnt : services) {
            System.out.println(prnt.getName());
        }
        System.out.println("02-*********************");
        PrinterName printerName = new PrinterName(services[0].getName(), null); //gets printer 
        System.out.println("03-*********************");
        PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
        printServiceAttributeSet.add(printerName);
        System.out.println("04-*********************");
        JRPrintServiceExporter exporter = new JRPrintServiceExporter();
        System.out.println("05-*********************");
        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();
    }

    public String creerRapportUniqueProjet() {

        try {

            Map<String, Object> param = new HashMap<>();
            param.put("rapportNom", "Projet");
            param.put("PAR_IMAGE_BACKGROUND", "/reporting/sources/BackgroundElit.png");
            param.put("PAR_IMAGE_HEADER", "/reporting/sources/headerElit.png");

            System.out.println("1-*********************");
            JRDataSource dataSource = new JRBeanCollectionDataSource(reportingProjetViewFacade.findById(projet));
            System.out.println("2-*********************");
            JasperPrint print = JasperFillManager.fillReport(getClass().getResourceAsStream("/reporting/sources/projectAllDetail.jasper"), param, dataSource);
            System.out.println("3-*********************");
            //JasperPrintManager.printPage(jp, 0, false);
            //JasperPrint jp =reportEngine.fillReport() ;//it returns stream 
            PrintReportToPrinter(print);//call method
            System.out.println("4-*********************");
        } catch (JRException e) {
            e.printStackTrace();
            System.out.println("Exception = " + e);
        }

        return "";
    }
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • 1
    also this is relevant http://stackoverflow.com/questions/8733276/how-to-open-print-dialog-after-pdf-generated – Petter Friberg Feb 13 '17 at 16:42
  • u are wrong ser, it's not duplicated, i have found the solution after more then a week, its just add one propertie to the jrxml file, this propertie is: /* Property name */ net.sf.jasperreports.export.pdf.javascript /* Property value */ this.print({bUI: true,bSilent: true,bShrinkToFit: false}); –  Feb 14 '17 at 10:02
  • answer only 1 question (the one you like most, the best on), then flag the others as duplicate (do not answer many questions, with same answer) See [Is it acceptable to add a duplicate answer to several questions?](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-add-a-duplica‌​te-answer-to-several-questions) – Petter Friberg Feb 14 '17 at 10:43

1 Answers1

-2

I have met the same problem and solved it with a table in my sgbd with parameters the ip of the client, the type of document (ie an a4 doc or a label with barcode) and the name of the printer to use.

I have a servlet dedicated to print, display or save the output of my reports ; you have to install all your printers on your server.

i know this could not work over the internet but for an office only app, it works well.

willyodin
  • 1
  • 1
  • 2
    Question is about solving it client side, solution is that you can't print directly but you can included javascript in pdf to open print dialog, see duplicate and my additional comment – Petter Friberg Feb 13 '17 at 16:41