Introduction:
In many web applications that use JasperReports, the same JSP export logic serves multiple report templates (.jrxml files). Each report may represent different business documents such as share certificates, membership forms, loan applications, or settlement forms. For a better user experience, the displayed heading and PDF metadata should clearly reflect the type of report being viewed or downloaded.
The following technologies have been used in enhancing jasper reports with dynamic PDF titles.
- Java
- JasperReport
Why we need to do: –
In When users open a PDF in the browser, the tab title or heading should clearly reflect the actual document type, such as loan Form 101 , instead of a generic filename or JSP name. This makes it easier for them to immediately recognize the purpose of the file without guessing. When downloading, descriptive filenames help users store and identify reports more efficiently in their folders.Distinct titles are especially useful when multiple reports are opened at the same time, preventing confusion. Overall, dynamic headings improve clarity, organization, and the overall user experience..
How do we solve:
Step 1 – Identify the Report Type
Use the pdfName parameter (or another identifier) in the JSP to detect which .jrxml template is being compiled.
Step 2 – Generate a Dynamic Filename
Create a dynamicFileName variable using a switch or if block to map each pdfName to a descriptive, user-friendly title. You can also include key parameters (e.g., member number) to make the name unique and meaningful.
Step 3 – Apply the Filename to Downloads
Set the HTTP Content-Disposition header to “inline” or “attachment” with the dynamicFileName so that users see the correct name when saving or opening the file.
Step 4 – Set PDF Metadata Title (Optional but Recommended)
Use SimplePdfExporterConfiguration.setMetadataTitle(dynamicFileName) so the browser tab and PDF document properties show the correct title.
Step 5 – Keep Logic Centralized
Store all report name mappings in one place, so adding new report types only requires a single code change.
Code
Update the JSP file with the following modifications
// Import required packages <%@ page import="net.sf.jasperreports.engine.*, java.io.*, java.util.*" %> <%@ page import="net.sf.jasperreports.export.*" %> <%@ page import="net.sf.jasperreports.engine.export.*" %> // Generate a dynamic file name based on the Jasper file String dynamicFileName; switch (pdfName) { case "loanApplicationForm": // jrxml file name dynamicFileName = " loan Application " + parameter1 + ".pdf"; break; default: dynamicFileName = pdfName + ".pdf"; } // Set the response header so the browser uses the dynamic file name response.setHeader("Content-Disposition", "inline; filename=\"" + dynamicFileName + "\""); response.setContentType("application/pdf"); // Export the Jasper report with metadata so the browser tab title matches OutputStream outputStream = response.getOutputStream(); JRPdfExporter exporter = new JRPdfExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream)); SimplePdfExporterConfiguration config = new SimplePdfExporterConfiguration(); config.setMetadataTitle(dynamicFileName); // PDF tab title + document properties config.setMetadataAuthor("Your Company Name"); exporter.setConfiguration(config); // Send PDF to the browser exporter.exportReport(); outputStream.flush(); outputStream.close();
Conclusion :-
Implementing dynamic headings in Jasper Reports ensures that PDFs are both user-friendly and professionally presented. By matching the document title to its content, users can instantly identify the report’s purpose. This approach makes saving, organizing, and retrieving files much easier. It also enhances the browsing experience by displaying meaningful titles in the browser tab. In short, dynamic headings create a seamless, organized, and polished reporting experience.
`