将jasper pdf直接发送到Web应用程序中的打印机 [英] Send pdf jasper straight to the printer in web app

查看:54
本文介绍了将jasper pdf直接发送到Web应用程序中的打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要直接将pdf碧玉发送给打印机,当前代码PDF委托给浏览器,因此用户可以根据需要打印任意数量的副本.必须只允许打印一份,所以我认为我将直接发送到打印. 我在论坛上进行了搜索,但不知道什么是解决该问题的最佳解决方案.

看看我的代码:

public class UtilRelatorios {

public static void imprimeRelatorio(String relatorioNome,
        HashMap parametros) throws IOException, JRException {
        FacesContext fc = FacesContext.getCurrentInstance();
        ServletContext context = (ServletContext) fc.getExternalContext().getContext();
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        JasperPrint jasperPrint = 
                JasperFillManager.fillReport(
                        context.getRealPath("/relatorios")+ File.separator+relatorioNome+".jasper",
                        parametros);    
        //int finalPag = jasperPrint.getPages().size();
        //System.out.println("page: "+finalPag);
        //JasperPrintManager.printPage(jasperPrint,finalPag,false);
        byte[] b = null;
        //JasperPrintManager.printPage(jasperPrint, 0, false);

        try {
            b = JasperExportManager.exportReportToPdf(jasperPrint);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }    

            if (b != null && b.length > 0) {
                // Envia o relatório em formato PDF para o browser
                response.setContentType("application/pdf");
                int codigo = (int) (Math.random()*1000);
                response.setHeader("Content-disposition","inline);filename=relatorio_"+codigo+".pdf");
                response.setContentLength(b.length);
                ServletOutputStream ouputStream = response.getOutputStream();
                ouputStream.write(b, 0, b.length);
                ouputStream.flush();
                ouputStream.close();
            }   
 }

}

解决方案

如果有疑问,您希望通过网络应用程序直接将报告 发送到用户的打印机, browser.

这可能不能完成!,您不能直接从浏览器控制网络用户打印机(不使用ActiveX或其他自制插件)

这很可能是幸运的,因为否则在互联网上导航时,您会看到人们在打印机上打印很多广告....

如果您想将其发送到服务器上的打印机,则可以这样做!

如果它是服务器打印机,请告诉我,我可以为您传递一些代码.

I need to send a pdf jasper directly to the printer, the current code PDF is delegated to the browser and therefore the user can print as many copies as desired. Must allow only print one copy, so I thought I'd send directly to printing. I searched the forum but did not understand what would be the best solution to the issue.

Take a look at my code:

public class UtilRelatorios {

public static void imprimeRelatorio(String relatorioNome,
        HashMap parametros) throws IOException, JRException {
        FacesContext fc = FacesContext.getCurrentInstance();
        ServletContext context = (ServletContext) fc.getExternalContext().getContext();
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        JasperPrint jasperPrint = 
                JasperFillManager.fillReport(
                        context.getRealPath("/relatorios")+ File.separator+relatorioNome+".jasper",
                        parametros);    
        //int finalPag = jasperPrint.getPages().size();
        //System.out.println("page: "+finalPag);
        //JasperPrintManager.printPage(jasperPrint,finalPag,false);
        byte[] b = null;
        //JasperPrintManager.printPage(jasperPrint, 0, false);

        try {
            b = JasperExportManager.exportReportToPdf(jasperPrint);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }    

            if (b != null && b.length > 0) {
                // Envia o relatório em formato PDF para o browser
                response.setContentType("application/pdf");
                int codigo = (int) (Math.random()*1000);
                response.setHeader("Content-disposition","inline);filename=relatorio_"+codigo+".pdf");
                response.setContentLength(b.length);
                ServletOutputStream ouputStream = response.getOutputStream();
                ouputStream.write(b, 0, b.length);
                ouputStream.flush();
                ouputStream.close();
            }   
 }

}

解决方案

If as seems in question you like to send the report directly to user's printer via web application, browser.

This can not be done!, you can not control the web users printer directly from the browser (excluding the use of activeX or other home made plugins)

Probably this is luck since otherwise while navigating on internet you would have people printing alot of advertising on your printer....

If instead you like to send it to a printer attached to server, this can be done!

If its the server printer please let me know and I can pass you some code.

这篇关于将jasper pdf直接发送到Web应用程序中的打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆