You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Controller生成Jasper Report空白PDF问题求助

Fixing Blank Jasper Report PDF in Spring Boot

Here are the key issues in your code and actionable fixes:

1. Missing Data Source for the Report

Your JasperFillManager.fillReport() call only passes parameters, but if your test.jrxml includes a detail band that relies on dynamic data (like database records or object lists), you must provide a data source. Without it, the report will render empty if there's no static content in title/summary bands.

Fix:

If using a list of objects as data:

// Replace with your actual data retrieval logic
List<YourDataModel> reportData = yourService.fetchDataById(id);
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(reportData);

// Pass data source to the fill method
JasperPrint jasperPrint = JasperFillManager.fillReport(compileReport, parameters, dataSource);

If using a JDBC database connection:

@Autowired
private DataSource dataSource;

// Inside the method
try (Connection connection = dataSource.getConnection()) {
    JasperPrint jasperPrint = JasperFillManager.fillReport(compileReport, parameters, connection);
}

2. Incorrect JRXML Path Handling

Using src/main/resources/... with FileInputStream will fail when your app is packaged into a JAR, as resources are embedded inside the JAR file, not as standalone filesystem files.

Fix:

Use Spring's ResourceLoader to load the JRXML correctly:

@Autowired
private ResourceLoader resourceLoader;

// Inside the method
Resource resource = resourceLoader.getResource("classpath:templates/pages/user/test.jrxml");
InputStream inputStream = resource.getInputStream();
JasperReport compileReport = JasperCompileManager.compileReport(inputStream);

3. Verify JRXML Template Content

Double-check your test.jrxml has actual content (static text, text fields, images) in bands like Title, Detail, or Summary. If all bands are empty, the PDF will be blank regardless of code fixes.

4. Optional: Adjust Content-Disposition Header

If you want to force a download instead of inline viewing, switch to attachment:

headers.add("Content-Disposition", "attachment; filename=invoice.pdf");

Updated Full Code Example

@Autowired
private ResourceLoader resourceLoader;
@Autowired
private YourDataService yourDataService;

@GetMapping(value = "/report/pdf/{id}", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> downloadInvoice(@PathVariable Long id) throws JRException, IOException {
    // Load JRXML resource
    Resource resource = resourceLoader.getResource("classpath:templates/pages/user/test.jrxml");
    InputStream inputStream = resource.getInputStream();
    JasperReport compileReport = JasperCompileManager.compileReport(inputStream);
    
    // Prepare parameters (add any required parameters here)
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("invoiceId", id);
    
    // Fetch report data
    List<YourDataModel> reportData = yourDataService.getDataById(id);
    JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(reportData);
    
    // Fill report with data
    JasperPrint jasperPrint = JasperFillManager.fillReport(compileReport, parameters, dataSource);
    
    // Export to PDF
    byte[] data = JasperExportManager.exportReportToPdf(jasperPrint);
    
    // Set response headers
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", "attachment; filename=invoice.pdf");
    
    return ResponseEntity.ok()
            .headers(headers)
            .contentType(MediaType.APPLICATION_PDF)
            .body(data);
}

Notes:

  • Replace YourDataModel and yourDataService with your actual data models and business logic.
  • Ensure JRXML field names match the property names in your data class (case-sensitive).
  • Confirm you have all required Jasper Reports dependencies in your pom.xml or build.gradle.

内容的提问来源于stack exchange,提问作者Phortsophea MMO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:03:16