Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.util.HashMap;
- import java.util.Map;
- import javax.print.PrintService;
- import javax.print.PrintServiceLookup;
- 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.JREmptyDataSource;
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JRExporterParameter;
- import net.sf.jasperreports.engine.JasperCompileManager;
- import net.sf.jasperreports.engine.JasperFillManager;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.JasperPrintManager;
- import net.sf.jasperreports.engine.JasperReport;
- import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
- import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
- public class PrintApp {
- public static void main(String[] args) throws Exception {
- PrintApp app = new PrintApp();
- app.testPrinting();
- }
- private void testPrinting() throws JRException {
- PrintService impressora = PrintServiceLookup.lookupDefaultPrintService();
- System.out.println("get report template");
- InputStream templateAsStream = ClassLoader.getSystemResourceAsStream("printertest.jrxml");
- System.out.println("compile report");
- JasperReport jasperReport = JasperCompileManager.compileReport(templateAsStream);
- // submit parameters
- Map parameters = new HashMap();
- parameters.put("param1", "jasper report ...");
- parameters.put("param2", "...rules");
- System.out.println("fill the compiled template");
- JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
- PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
- PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
- //printServiceAttributeSet.add(MediaSizeName.ISO_A4);
- printServiceAttributeSet.add(new PrinterName("Zebra105SL-1", null));
- printRequestAttributeSet.add(new Copies(1));
- JRPrintServiceExporter exporter = new JRPrintServiceExporter();
- exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
- 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);
- System.out.println("export to printer");
- exporter.exportReport();
- // JasperPrintManager.printReport(jasperPrint, false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment