Direct Print to Non-standard Printer with Jasperprintmanager

Asked

Viewed 620 times

1

In my system a txt file is imported read and printed on the standard printer, the report is done in ireport and I use the printPage method of Jasperprintmanager to print on the standard printer, according to the code below:

public void ImprimirBilhete(java.util.List lista) {

        /*String caminhoRelJasper = "/com/bilhete/relatorio/bilheteteste.jasper";

        InputStream relJasper = getClass().getResourceAsStream(caminhoRelJasper);*/
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(lista);

        Map parametros = new HashMap();
        JasperPrint impressao = null;
        //parametros.put("CAMINHO_IMAGEM", System.getProperty("user.dir") + "\\imagem\\logocidade.jpg");

        try {
            //parametros.put("CAMINHO_IMAGEM", System.getProperty("user.dir") + "\\imprimir\\barra.jpeg");
            //*para funcionario abrir os relatorio dentro do pacote tem q configurar o parametros no ireport como InputScream
            InputStream caminhoImagemBrasao = getClass().getResourceAsStream("/com/bilhete/imagens/barra.jpeg");
            parametros.put("CAMINHO_IMAGEM", caminhoImagemBrasao);
            InputStream is = getClass().getResourceAsStream("/com/bilhete/relatorio/bilheteteste.jasper");
            impressao = JasperFillManager.fillReport(is, parametros, ds);
            //impressao = JasperFillManager.fillReport(relJasper, parametros, ds);
            //JasperViewer viewer = new JasperViewer(impressao, true);
            //*aparece o relatorio na tela
            //*JasperViewer.viewReport(impressao, false);
            //viewer.setVisible(true);
            //manda direto pra impressora padrao
            JasperPrintManager.printPage(impressao, 0, false);

        } catch (JRException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e, "Erro Ireport", JOptionPane.INFORMATION_MESSAGE);
        }
    }

I wanted to know if there is any method in Jasperprintmanager that sends the printing to a specific printer, such as a printer with the name "such" or some USB port.

1 answer

1


Hello, you can do that. Using the import java.awt.print.Printerjob;

try {

    String report = JasperCompileManager.compileReportToFile(sourceFileName);

    JasperPrint jasperPrint = JasperFillManager.fillReport(report, para, ds);

    PrinterJob printerJob = PrinterJob.getPrinterJob();

    PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
    printerJob.defaultPage(pageFormat);

    int selectedService = 0;

    AttributeSet attributeSet = new HashPrintServiceAttributeSet(new PrinterName(printerNameShort, null));

    PrintService[] printService = PrintServiceLookup.lookupPrintServices(null, attributeSet);

    try {
        printerJob.setPrintService(printService[selectedService]);

    } catch (Exception e) {

        System.out.println(e);
    }
    JRPrintServiceExporter exporter;
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
    printRequestAttributeSet.add(new Copies(1));

    // these are deprecated
    exporter = new JRPrintServiceExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, printService[selectedService]);
    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printService[selectedService].getAttributes());
    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
    exporter.exportReport();

} catch (JRException e) {
    e.printStackTrace();
}

I got the answer from this place:

https://stackoverflow.com/a/31182146/3767352

  • Thanks I’ll take a look

  • @jallissonjallisoliveiraband, I adjusted the responsta, I believe with that already solve your problem.

  • 1

    valeu helped me a lot I used the private void Printreporttoprinter(Jasperprint jp) throws Jrexception { from the example.

  • Whoa, that’s great. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.