使用ireport打印数据,servlet里面的固定写法。。。

来源:互联网 发布:vue.js input不可编辑 编辑:程序博客网 时间:2024/06/05 21:59
if ("html".equals(condition)) {
                PrintWriter pw = response.getWriter();
                response.setContentType("text/html;charset=UTF-8");
                response.setHeader("pragma", "no-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);
                JRHtmlExporter html = new JRHtmlExporter();
                html.setParameter(JRExporterParameter.JASPER_PRINT, print);
                html.setParameter(JRExporterParameter.OUTPUT_WRITER, pw);
                html.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
                html.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./images/");
                html.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME,
                        this.getServletContext().getRealPath("/images") + "\\");
                html.exportReport();
                pw.flush();
                pw.close();
            } else if ("pdf".equals(condition)) {
                response.setContentType("application/pdf;charset=utf-8");
                response.setHeader("Content-Disposition", "inline;filename=\""// attachment,inline
                        + type + ".pdf\"");
                JRExporter exporter = new JRPdfExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
                exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
                exporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, Boolean.TRUE);
                exporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY, Boolean.TRUE);
                exporter.setParameter(JRPdfExporterParameter.PERMISSIONS,
                        Integer.valueOf(String.valueOf(PdfWriter.AllowCopy | PdfWriter.AllowPrinting)));
                exporter.exportReport();
            }
0 0