我在我的应用程序中使用Jasper报告。
步骤是基本步骤,
response.setHeader("Content-Disposition", "attachment;filename=report.pdf");
response.setContentType("application/pdf");
OutputStream outStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(new File(this.template)); // .jrxml file
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(this.dataList);
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint,outStream );
JasperExportManager.exportReportToPdfFile(jasperPrint,"/test.pdf");// this works in any case
我在HTML页面中提交了表单。
<form action="{{vm.downloadUrl}}"
id="downloadPdf" name="downloadPdf"
method="post"></form>
js是,(这将下载pdf没有问题)
vm.downloadUrl = appConfig.baseUrl+'/generateReport2?data='+data;
document.getElementById('downloadPdf').action = vm.downloadUrl;
document.downloadPdf.submit(); // document.form_name.submit();
但是,当我使用下面的代码时,它将下载一个空的pdf,尽管PDF创建并从JasperExportManager.exportReportToPdfFile(jasperPrint,"/test.pdf");
$http.post(vm.downloadUrl)
.success(function (response) {
console.log('response received :');
console.log(response);
var file = new Blob([response], { type: 'application/pdf' });
saveAs(file, 'filename.pdf');
});
这里有什么问题。为什么要下载空文件。我之前遇到过类似的问题,现在通过在jrxml文件中设置whenNoDataType="AllSectionsNoDetail"
来处理它。