Java 在web应用程序中将Jasper报表导出为PDF输出流

Java 在web应用程序中将Jasper报表导出为PDF输出流,java,spring-boot,stream,jasper-reports,Java,Spring Boot,Stream,Jasper Reports,我在一个网站上工作,它应该会生成报告。 到目前为止,我已经有了将报告导出到PDF输出流的代码,但是我想知道如何将PDF返回给用户,或者如何查看它以查看它是否工作 InputStream template = getClass().getResourceAsStream(invoice_template_path); JasperReport report = JasperCompileManager.compileReport(template); Map&

我在一个网站上工作,它应该会生成报告。 到目前为止,我已经有了将报告导出到PDF输出流的代码,但是我想知道如何将PDF返回给用户,或者如何查看它以查看它是否工作

    InputStream template = 
    getClass().getResourceAsStream(invoice_template_path);

    JasperReport report = JasperCompileManager.compileReport(template);

    Map<String,Object> param = parameters(reservation);

    JasperPrint print = JasperFillManager.fillReport(report, param);

    File pdf = File.createTempFile("output","pdf");
    JasperExportManager.exportReportToPdfStream(print,new FileOutputStream(pdf));
InputStream模板=
getClass().getResourceAsStream(发票模板路径);
JasperReport report=jaspecompilemanager.compileReport(模板);
Map param=参数(保留);
JasperPrint print=JasperFillManager.fillReport(报告,参数);
File pdf=File.createTempFile(“输出”、“pdf”);
导出报告topdfstream(打印,新文件输出流(pdf));

您可以使用JRPdfExporter,然后将JasperPrint变量传递给setExporterInput。下面的代码提供了更多详细信息:

@GetMapping( produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> getReport( ) throws Exception {
        /*
         here  you prepare your: JasperPrint print
       */

        //initiate exporter
        JRPdfExporter exporter = new JRPdfExporter();
        ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
        exporter.setExporterInput(new SimpleExporterInput(print));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfOutputStream));

        SimplePdfReportConfiguration reportConfig = new SimplePdfReportConfiguration();
        reportConfig.setSizePageToContent(true);
        reportConfig.setForceLineBreakPolicy(false); 
      
            exporter.exportReport();
            res= pdfOutputStream.toByteArray();
   
        var headers = new HttpHeaders();
        headers.add("Content-Disposition", "inline; filename= filename.pdf");
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentType(MediaType.APPLICATION_PDF)
                .body(res);
    }

@GetMapping(products=MediaType.APPLICATION\u PDF\u值)
public ResponseEntity getReport()引发异常{
/*
在这里,您准备您的:JasperPrint印花
*/
//发起出口商
JRPdfExporter exporter=新的JRPdfExporter();
ByteArrayOutputStream pdfOutputStream=新建ByteArrayOutputStream();
exporter.setExporterInput(新的SimpleExporterInput(打印));
exporter.setExporterOutput(新的SimpleOutputStreamExportRoutPut(pdfOutputStream));
SimplePdfReportConfiguration reportConfig=新的SimplePdfReportConfiguration();
reportConfig.setSizePageToContent(true);
reportConfig.setForceLineBreakPolicy(false);
出口商。出口报告();
res=pdfOutputStream.toByteArray();
var headers=新的HttpHeaders();
添加(“内容处置”,“内联;文件名=filename.pdf”);
返回响应性
.ok()
.标题(标题)
.contentType(MediaType.APPLICATION\u PDF)
.机构(res);
}

您可能已经有了答案,但这可能会帮助其他人…

您可以使用JRPdfExporter,然后将JasperPrint变量传递给setExporterInput。下面的代码提供了更多详细信息:

@GetMapping( produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> getReport( ) throws Exception {
        /*
         here  you prepare your: JasperPrint print
       */

        //initiate exporter
        JRPdfExporter exporter = new JRPdfExporter();
        ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
        exporter.setExporterInput(new SimpleExporterInput(print));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfOutputStream));

        SimplePdfReportConfiguration reportConfig = new SimplePdfReportConfiguration();
        reportConfig.setSizePageToContent(true);
        reportConfig.setForceLineBreakPolicy(false); 
      
            exporter.exportReport();
            res= pdfOutputStream.toByteArray();
   
        var headers = new HttpHeaders();
        headers.add("Content-Disposition", "inline; filename= filename.pdf");
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentType(MediaType.APPLICATION_PDF)
                .body(res);
    }

@GetMapping(products=MediaType.APPLICATION\u PDF\u值)
public ResponseEntity getReport()引发异常{
/*
在这里,您准备您的:JasperPrint印花
*/
//发起出口商
JRPdfExporter exporter=新的JRPdfExporter();
ByteArrayOutputStream pdfOutputStream=新建ByteArrayOutputStream();
exporter.setExporterInput(新的SimpleExporterInput(打印));
exporter.setExporterOutput(新的SimpleOutputStreamExportRoutPut(pdfOutputStream));
SimplePdfReportConfiguration reportConfig=新的SimplePdfReportConfiguration();
reportConfig.setSizePageToContent(true);
reportConfig.setForceLineBreakPolicy(false);
出口商。出口报告();
res=pdfOutputStream.toByteArray();
var headers=新的HttpHeaders();
添加(“内容处置”,“内联;文件名=filename.pdf”);
返回响应性
.ok()
.标题(标题)
.contentType(MediaType.APPLICATION\u PDF)
.机构(res);
}
您可能已经有了答案,但这可能会帮助其他人