Java 中使用 JasperReports 生成大型 PDF-Java专区论坛-技术-SpringForAll社区

Java 中使用 JasperReports 生成大型 PDF

JasperReports 是一个开源的 Java 报表工具,允许用户创建、设计和生成各种格式的报表,如 PDF、HTML、Excel、CSV 和 XML。它广泛用于从多种数据源生成动态内容,包括数据库、XML 文件和自定义数据源,是 Crystal Reports 的替代品。

问题

在这个场景中,开发者生成月度或年度的 PDF 报告。一个报告包含大量数据,消耗逻辑内存。

在另一个场景中,一个应用程序处理许多客户,同时生成实时报告,消耗逻辑内存。

开发者在生成大型 PDF 时遇到的一个标准错误。

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space

示例:使用 JasperReports 生成 PDF 报告。

在 pom.xml 中添加依赖项。

<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
  <dependency>
   <groupId>net.sf.jasperreports</groupId>
   <artifactId>jasperreports</artifactId>
   <version>7.0.0</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports-fonts -->
  <dependency>
   <groupId>net.sf.jasperreports</groupId>
   <artifactId>jasperreports-fonts</artifactId>
   <version>7.0.0</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports-pdf -->
  <dependency>
   <groupId>net.sf.jasperreports</groupId>
   <artifactId>jasperreports-pdf</artifactId>
   <version>7.0.0</version>
  </dependency>

创建一个 JRXML 文件使用 Jaspersoft Studio。开发者可以下载 Jaspersoft Studio。

d2b5ca33bd20240729100000

创建 JRXML 以生成用户活动数据的月度 PDF 报告。

d2b5ca33bd20240729100033

创建参数和字段。

d2b5ca33bd20240729100042

开发者可以添加更多元素来学习和练习 JasperReports。

d2b5ca33bd20240729100105

在设计选项卡中,开发者可以设计报告的模板。
在源代码选项卡中,开发者可以查看报告的源代码。
在预览选项卡中,开发者可以预览报告。

d2b5ca33bd20240729100127

<!-- Created with Jaspersoft Studio version 7.0.0.final using JasperReports Library version 7.0.0-b478feaa9aab4375eba71de77b4ca138ad2f62aa  -->
<jasperReport name="Blank_A4" language="java" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2385a4f5-3779-4dca-9895-c6682d2a919c">
 <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
 <parameter name="P_MONTH" class="java.lang.String"/>
 <parameter name="P_YEAR" class="java.lang.String"/>
 <query language="sql"><![CDATA[]]></query>
 <field name="userId" class="java.lang.String"/>
 <field name="activityType" class="java.lang.String"/>
 <field name="activityTime" class="java.lang.String"/>
 <field name="ipAddress" class="java.lang.String"/>
 <field name="deviceType" class="java.lang.String"/>
 <field name="location" class="java.lang.String"/>
 <field name="browserType" class="java.lang.String"/>
 <field name="operatingSystem" class="java.lang.String"/>
 <background splitType="Stretch"/>
 <title height="48" splitType="Stretch">
  <element kind="staticText" uuid="c89c2c18-55e5-4446-9a82-25e6f5c0f06b" x="170" y="0" width="250" height="40" fontSize="26.0">
   <text><![CDATA[User Activity Report]]></text>
  </element>
 </title>
 <pageHeader height="20" splitType="Stretch">
  <element kind="textField" uuid="f98a5196-d2e6-44f9-97aa-65e3ce5c92fc" x="0" y="0" width="120" height="20">
   <expression><![CDATA["MONTH "+$P{P_MONTH}]]></expression>
  </element>
  <element kind="textField" uuid="264db46d-a662-4b6a-b659-89912f4e7093" x="120" y="0" width="120" height="20">
   <expression><![CDATA["YEAR "+$P{P_YEAR}]]></expression>
  </element>
 </pageHeader>
 <columnHeader height="30" splitType="Stretch">
  <element kind="staticText" uuid="33d063a8-53cd-47c2-ac6e-fc5e10c0e1fa" x="0" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[User ID]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="096f62de-e9cf-4eb1-9569-b474dd2bc5d0" x="70" y="10" width="80" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Activity Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="e144d215-722b-42a0-9c4c-e9d4ad1e3a08" x="230" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[IP Address]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="d32f3554-1c78-43dd-9618-f02f8ecca6b3" x="300" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Device Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="e44b90e9-3c69-43e3-a8d8-262c1af32d08" x="370" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Location]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="30a6bd46-013a-42dd-8d02-0046b54df694" x="440" y="10" width="74" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Browser Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="1f18e96c-5bca-4a9d-9192-f8caacb60d2d" x="514" y="10" width="41" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[OS]]></text>
   <property name="com.jaspersoft.studio.unit.width" value="px"/>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="b150b5c6-b1d0-4142-b366-897aad526336" x="150" y="10" width="80" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Activity Time]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
 </columnHeader>
 <detail>
  <band height="20" splitType="Stretch">
   <element kind="textField" uuid="54d90e0d-3ebd-49e2-ad11-c1177887bb42" x="0" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{userId}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="a85f2022-27a6-44cc-bedc-8dcfae88041f" x="70" y="0" width="80" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{activityType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="59d4a2f7-8724-4eaf-b9df-2ddcb434ae67" x="150" y="0" width="80" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{activityTime}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="598eff31-517f-42a2-9285-8a7304f5898e" x="230" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{ipAddress}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="09dcad81-7f6d-47ae-b26e-64560565dd5a" x="300" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{deviceType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="8ee606b7-fa2b-4517-9ba2-16e6b111eee3" x="370" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{location}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="2c15f86e-d3c2-48e8-bfa8-79a4e52b2d4b" x="440" y="0" width="74" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{browserType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="32f657d8-0867-4df7-9c3e-16fcff6ea6d0" x="514" y="0" width="41" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{operatingSystem}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <property name="com.jaspersoft.studio.unit.width" value="px"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
  </band>
 </detail>
 <columnFooter splitType="Stretch"/>
 <pageFooter height="30" splitType="Stretch">
  <element kind="textField" uuid="51dd90d5-1007-42db-9e97-7131585b309f" x="355" y="0" width="100" height="30" hTextAlign="Right">
   <expression><![CDATA["Page " + $V{PAGE_NUMBER}]]></expression>
  </element>
  <element kind="textField" uuid="80945c25-fde6-4116-8c2f-2e7d252c69f7" x="455" y="0" width="100" height="30" evaluationTime="Report" hTextAlign="Left">
   <expression><![CDATA[" of " + $V{PAGE_NUMBER}]]></expression>
  </element>
 </pageFooter>
 <summary height="7" splitType="Stretch"/>
</jasperReport>

创建用于报告详细带的 UserActivity bean。

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UserActivity {
    private String userId;
    private String activityType;
    private String activityTime;
    private String ipAddress;
    private String deviceType;
    private String location;
    private String browserType;
    private String operatingSystem;
}

创建 GeneratePDFJasperReport 类以生成 PDF 报告。

package com.example.demo.sandbox;

import com.example.demo.bean.UserActivity;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GeneratePDFJasperReport {

    public static void main(String[] args) {
        generateReport();
    }

    public static void generateReport(){
        try {
            // Compile the Jasper report from .jrxml to .jasper
            String sourceFileName = "C:/JaspersoftWorkspace/MyReports/demo_large.jrxml";
            JasperReport jasperReport = JasperCompileManager.compileReport(sourceFileName);

            // Generate sample data
            List<UserActivity> activities = generateUserActivities();

            // Create a JRDataSource from the list
            JRDataSource jrDataSource = new JRBeanCollectionDataSource(activities);

            // Parameters for the report
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("P_MONTH","JULY");
            parameters.put("P_YEAR","2024");

            // Fill the report
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);

            // Export the report to a PDF file
            String outputFileName = "D:/pdf/report.pdf";
            JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);

            System.out.println("Report generated successfully!");
        } catch (JRException e) {
            e.printStackTrace();
        }
    }

    public static List<UserActivity> generateUserActivities() {
        List<UserActivity> activities = new ArrayList<>();

        for (int i = 1; i <= 10; i++) {
            UserActivity activity = new UserActivity();
            activity.setUserId(String.valueOf(i));
            activity.setActivityType("activity");
            activity.setActivityTime("2024-07-07 10:00");
            activity.setIpAddress("192.168.0.1");
            activity.setDeviceType("PC");
            activity.setLocation("US");
            activity.setBrowserType("CHROME");
            activity.setOperatingSystem("WINS");

            activities.add(activity);
        }

        return activities;
    }

}

使用 Intelij 测试生成 PDF

执行 GeneratePDFJasperReport 主方法以生成 PDF。

d2b5ca33bd20240729100327

在这个示例中,JasperReports 仅使用逻辑内存。

当用户活动数据有数十万或数百万条记录时,会导致错误“OutOfMemoryError”。

监控 Java 应用程序的内存使用情况

为了监控逻辑内存,开发者可以使用 IntelliJ IDEA Ultimate 在 IntelliJ IDEA 中进行分析。

另一个选项是免费的,VisualVM,一个集成了命令行 JDK 工具和轻量级分析功能的可视化工具。

生成大型 PDF

为了模拟“OutOfMemoryError”的情况,通过减少内存,添加 VM 选项 -Xmx256m -Xms128m 并将用户活动数据增加到 20 万条记录。

d2b5ca33bd20240729100424

public static List<UserActivity> generateUserActivities() {
    List<UserActivity> activities = new ArrayList<>();

    for (int i = 1; i <= 200000; i++) {
        UserActivity activity = new UserActivity();
        activity.setUserId(String.valueOf(i));
        activity.setActivityType("activity");
        activity.setActivityTime("2024-07-07 10:00:00");
        activity.setIpAddress("192.168.0.1");
        activity.setDeviceType("PC");
        activity.setLocation("US");
        activity.setBrowserType("CHROME");
        activity.setOperatingSystem("WINS");

        activities.add(activity);
    }

    return activities;
}

打开 VisualVM 以监控内存使用情况。在执行“main”方法时,会导致 Java 堆空间错误。

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
Exception in thread “RMI TCP Connection(idle)” java.lang.OutOfMemoryError: Java heap space

VisualVM 显示内存使用情况。

d2b5ca33bd20240729100511

在生成 PDF 时,内存使用量达到峰值并导致 Java 堆空间错误。

JasperReports 提供了一个功能来解决这个问题。Virtualizer 通过使用物理内存来帮助生成大型 PDF,从而控制内存使用。

JRSwapFileVirtualizer:通过将数据从内存卸载到磁盘来管理内存。
JRFileVirtualizer:将数据写入多个临时文件,而不是单个交换文件。
JRGzipVirtualizer:在将数据写入磁盘之前压缩数据,这可以在压缩和解压缩的额外 CPU 使用成本下节省磁盘空间。

使用 Virtualizer 生成大型 PDF

JRSwapFileVirtualizer

public static void generateReportVirtualizer(){
    try {
        // Compile the Jasper report from .jrxml to .jasper
        String sourceFileName = "C:/JaspersoftWorkspace/MyReports/demo_large.jrxml";
        JasperReport jasperReport = JasperCompileManager.compileReport(sourceFileName);

        // Generate sample data
        List<UserActivity> activities = generateUserActivities();

        // Create a JRSwapFileVirtualizer
        JRSwapFile jrSwapFile = new JRSwapFile("C:/JaspersoftWorkspace/MyReports/",1024,1);
        JRSwapFileVirtualizer virtualizer = new JRSwapFileVirtualizer(20, jrSwapFile, true);

        // Create a JRDataSource from the list
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(activities);

        // Parameters for the report
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("P_MONTH","JUNE");
        parameters.put("P_YEAR","2024");
        parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);

        // Fill the report
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);

        // Export the report to a PDF file
        String outputFileName = "D:/pdf/report.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);

        System.out.println("Report generated successfully!");
    } catch (JRException e) {
        e.printStackTrace();
    }
}

运行“main”方法并使用 VisualVM 监控内存使用情况。

d2b5ca33bd20240729100611

d2b5ca33bd20240729100619

d2b5ca33bd20240729100626

报告成功生成,没有内存错误。

在 VisualVM 上,内存使用量没有达到峰值内存使用量。

开发者应该管理磁盘空间,因为 JRSwapFileVirtualizer 使用物理内存。生成报告后,开发者必须删除 JRSwapFileVirtualizer 创建的临时文件。

d2b5ca33bd20240729100712

JRGzipVirtualizer

public static void generateReportVirtualizer(){
    try {
        // Compile the Jasper report from .jrxml to .jasper
        String sourceFileName = "C:/JaspersoftWorkspace/MyReports/demo_large.jrxml";
        JasperReport jasperReport = JasperCompileManager.compileReport(sourceFileName);

        // Generate sample data
        List<UserActivity> activities = generateUserActivities();

        // Create a JRGzipVirtualizer
        JRGzipVirtualizer virtualizer = new JRGzipVirtualizer(50);

        // Create a JRDataSource from the list
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(activities);

        // Parameters for the report
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("P_MONTH","JUNE");
        parameters.put("P_YEAR","2024");
        parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);

        // Fill the report
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);

        // Export the report to a PDF file
        String outputFileName = "D:/pdf/report.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);

        System.out.println("Report generated successfully!");
    } catch (JRException e) {
        e.printStackTrace();
    }
}

运行“main”方法并使用 VisualVM 监控内存使用情况。

d2b5ca33bd20240729100747

报告成功生成,没有内存错误。

在 VisualVM 上,内存使用量没有达到峰值。然而,JRSwapFileVirtualizer 具有不同的内存使用模式。

JRFileVirtualizer

public static void generateReportVirtualizer(){
        try {
            // Compile the Jasper report from .jrxml to .jasper
            String sourceFileName = "C:/JaspersoftWorkspace/MyReports/demo_large.jrxml";
            JasperReport jasperReport = JasperCompileManager.compileReport(sourceFileName);

            // Generate sample data
            List<UserActivity> activities = generateUserActivities();

            // Create a JRFileVirtualizer
            int maxSize = 100; // Max number of pages in memory
            String tempDir = "C:/JaspersoftWorkspace/MyReports/"; // Temp directory for virtualizer files
            JRFileVirtualizer virtualizer = new JRFileVirtualizer(maxSize, tempDir);


            // Create a JRDataSource from the list
            JRDataSource jrDataSource = new JRBeanCollectionDataSource(activities);

            // Parameters for the report
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("P_MONTH","JUNE");
            parameters.put("P_YEAR","2024");
            parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);

            // Fill the report
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);

            // Export the report to a PDF file
            String outputFileName = "D:/pdf/report.pdf";
            JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);

            System.out.println("Report generated successfully!");
        } catch (JRException e) {
            e.printStackTrace();
        }
    }

运行“main”方法并使用 VisualVM 监控内存使用情况。

d2b5ca33bd20240729100845

报告成功生成,没有内存错误。

d2b5ca33bd20240729100858

在使用 JRFileVirtualizer 生成 PDF 时会创建一个临时文件。当 PDF 生成完成后,临时文件会被删除。

预编译

Jaspersoft Studio 提供了将“.jrxml”编译为“.jasper”的功能。

编译报告可以检测到一些错误。开发者可以在 Java 应用程序中实现 JasperReport 之前编译报告以检查错误。

d2b5ca33bd20240729100937

开发者可以使用“.jasper”而不是“.jrxml”来节省资源。

String jasperFile = "C:/JaspersoftWorkspace/MyReports/demo_large.jasper";
JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(jasperFile);

最后,每个 Virtualizer 都有优缺点。开发者必须根据单个报告的条件决定在生成 PDF 时使用哪个 Virtualizer。

请登录后发表评论

    没有回复内容