数据库中的JasperReport.jasper文件



我正在Springboot项目中使用jasperreports。我可以根据数据和资源文件夹中的.jasper文件生成PDF。但是,我的资源中不希望有.jasper文件。如果是这样,我想将.jasper作为字节数组保存在数据库中。

我不知道如何从附件中创建jasperReport对象,请给出建议。

这是我的控制器,用于返回创建的报告。

@GetMapping("api/V1/pdfDocument/{finDocId}")
public ResponseEntity<byte[]> generatePdf(@PathVariable long finDocId) throws Exception {
FinancialDocument financialDocument = financialDocumentService.getDocumentById(finDocId);
ArrayList<FinancialDocument> documentCollection = new ArrayList<>();
documentCollection.add(financialDocument);
JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(documentCollection);
Attachment reportAttachment = attachmentService.getAttachmentById(1L);
JasperReport jasperReport = new InputStream(....);
JasperPrint report = JasperFillManager.fillReport(jasperReport, null, beanCollectionDataSource);
byte[] data = JasperExportManager.exportReportToPdf(report);
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "inline;filename=invoice.pdf");
return ResponseEntity.ok().headers(headers).contentType(MediaType.APPLICATION_PDF).body(data);
//return null;
}

这是我的附件类

public class Attachment {
private Long attachmentId;
private String attachmentName;
private String attachmentType;
private byte[] attachmentFile;

}

只需获取包含Jasper报告设计的字节数组,并将其封装到ByteArrayInputStream:中

Attachment reportAttachment = attachmentService.getAttachmentById(1L);
ByteArrayInputStream inputStream = new ByteArrayInputStream(reportAttachment.attachmentFile);
JasperPrint report = JasperFillManager.fillReport(inputStream, null, beanCollectionDataSource);

最新更新