Jump to content

jasperreport printing


jaspernewuser

Recommended Posts

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

// -------CODE --------------------

private void printExport(JasperPrint print){
        JRExporter exporter = new JRPrintServiceExporter();   

        //--- Get printjob and service (default printer)
        PrinterJob pj = PrinterJob.getPrinterJob();
        PrintService ps = pj.getPrintService();

          //--- Set print properties
      PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
      printRequestAttributeSet.add(MediaSizeName.ISO_A4);

          //--- Set print parameters
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
      exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);     
      exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, ps.getAttributes());     
      exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
      exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);

          //--- Print the document
      try{
          exporter.exportReport();
      }
      catch(JRException e){
          e.printStackTrace();
      }
    }

// ---------- END OF CODE ---------

This should do the trick, if you have a jasperprint object ready.

good luck!

Link to comment
Share on other sites

 

 

Hey Nicholas,

   First Thanks for quick reply. I did what you said, still am getting error saying.

Exception in thread "main"

net.sf.jasperreports.engine.JRException: java.awt.print.PrinterException: Printer is not accepting job.JRPrintServiceExporter.java:253)PrintServiceApp.java:54)java.awt.print.PrinterException: Printer is not accepting job.RasterPrinterJob.java:1230)JRPrintServiceExporter.java:239)

Here is my sample code which I am using for testing purpose. also am uploading jrxml file.

JasperDesign jasperDesign = JRXmlLoader.load("C:\myexample\MySQLEmployeeReport.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://myservername/mydb", "dbuser", "dbpassword");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
jdbcConnection.close();

JRExporter exporter = new JRPrintServiceExporter();   
PrinterJob pj = PrinterJob.getPrinterJob();
PrintService ps = pj.getPrintService();
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(MediaSizeName.ISO_A4);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);     
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, ps.getAttributes());     
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
// application hangs here for some time and there gives error message
exporter.exportReport();

 


 

 

at net.sf.jasperreports.engine.export.JRPrintServiceExporter.exportReport(

at test.PrintServiceApp.main(

Caused by:

at sun.print.RasterPrinterJob.print(

at net.sf.jasperreports.engine.export.JRPrintServiceExporter.exportReport(

... 1 more

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...