Jasper Report. How to print to a file using a specified printer driver?

十年热恋 提交于 2019-12-13 04:55:10

问题


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:


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();
  }


来源:https://stackoverflow.com/questions/19894861/jasper-report-how-to-print-to-a-file-using-a-specified-printer-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!