Jasper Report. How to print to a file using a spec

2019-08-19 08:19发布

In Control Panel my printer's port was configured to "FILE:". For example, I can select a printer using PrintServiceAttributeSet, but there is no way to set an output filename when printing. I don't use JRXlsExporter or JRPdfExporter or something like that, just JRPrintServiceExporter.

  PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
  printServiceAttributeSet.add(new PrinterName("Xerox DocuPrint 100 EPS PS3", null));
  //...
  exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
  exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
  //...
  exporter.exportReport();

The driver outputs files in PostScript, but a window "Output file name" appears all the time. If I set a filename in the window manually, then printer prints to a file successfully.

Any idea how to set a filename programmatically?

1条回答
一夜七次
2楼-- · 2019-08-19 08:56

javax.print.attribute.standard » Destination helps.

Solution:

  //...
  import javax.print.attribute.standard.Destination;
  //...

  JRExporter exporter = new JRPrintServiceExporter();          

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

  //----------------------------------------------------     
  printRequestAttributeSet.add(new Destination(new java.net.URI("file:d:/output/report.ps")));
  //----------------------------------------------------     

  PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
  printServiceAttributeSet.add(new PrinterName("Xerox DocuPrint 100 EPS PS3", null)); 

  //--- Set print parameters      
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
  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);      

  //--- Print the document
  try{
      exporter.exportReport();
  }
  catch(JRException e){
      e.printStackTrace();
  }
查看更多
登录 后发表回答