• Development
  • Data Warehouse & BI
  • IT Architecture & Cloud Computing
  • Reporting tools
  • JasperReports
  • Generate a PDF File with JasperReports and Java

This tutorial will explain you how to Generate a PDF File from Java using Jasper Reports Library.

Stackextend

Generate PDF document using JasperReports and Spring boot

Java featured image

1. Overview

Almost every JAVA project need to generate PDF documents for its users, for example:

  • For an e-commerce project, we generate Invoice, receipt and return…
  • For a supervision and monitoring application, we generate reports.

The JasperReports is one of the best Java libraries for generating PDF documents.

In this article, I will show you how to generate PDF documents using JasperReports, Spring Boot and Jaspersoft Studio.

2. Implementation

Let’s suppose we are working on an e-commerce application (e.g. Hybris), and we want to generate the invoice as a PDF for our customers.

This is a simplified version of how we are going to set up the generation of the invoice using JasperReports.

Overview JasperReports project

  • I18n-fr.properties, i18n-en.properties, i18n.properties : is an i18n resource bundles used to translate the generated pdf file.
  • Template.jrxml : is a JasportReports template created using Jaspersoft Studio.
  • Invoice.pdf : is the final pdf document generated with the help of JasperReports.

2.1. JasperReports Template.

First of all, we need to create the JasperReports template of our invoice PDF document.

1. Download and install the Jaspersoft Studio.

2. Open the Jaspersoft Studio and create a JasperReports template using the toolbox that comes with it.

I have already generate a JasperReports template, you can find it in Github .
The JasperReports template is an XML file with extension .jrxml .

Jaspersoft studio jrxml template

Let’s shed light on some of the JRXML elements :

This code snippet will add an image to the final pdf file,  <![CDATA[$P{logo}]]> this parameter will be filled in Java with path to the image.

This code snippet adds a dynamic attribute to the pdf file, it will be filled in runtime with proper value: $P{order}.getAddress().getStreetName()

This one is similar to the last one, however this time the field value will be retrieved from the i18n resource bundle depending on the chosen languages (fr, en, it…).

2.2. Create i18n Resource Bundle

1. Create a file property for each language you need.

2. Extract all the static field from your jrxml template and add them (translated) to your files properties.

2.3. Create Spring Boot Project

1. Create a Spring Boot project using Spring Initializr .

2. Add the JasperReports and the Spring Support artifacts to your pom.xml .

  • JasperReports artifact
  • Spring Support artifact

3. Add the jrxml template, the i18n files properties and images… to the resources folder of your Spring Boot project.

Jasperreport project structure

2.4. Generate PDF in Java

1. Create a Java service  InvoiceService , we will use it to generate the PDF invoice.

OrderModel is a POJO object that holds all the data about the invoice, you can find it in Github .

2. Create a PDF File and initiate a FileOutputStream .

3. Load the JRXML template.

4. Fill the parameters and the DataSource.

5. Generate the PDF using the  JasperReportsUtils of the Spring Support.

6. Call your service  InvoiceService.generateInvoiceFor(...) with the proper values to generate the PDF.

7. If everything goes well the invoice as a PDF file will be generated.

Generated invoice pdf using JasperReports

Demo of the final version

Find the source code of this example in  GitHub .

jasper report to pdf java

Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of experience in Java/Kotlin, Java EE, Angular and Hybris… I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven architecture, Event Sourcing and Domain Driven design (DDD)… Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and BDD.

Share this:

guest

This site uses Akismet to reduce spam. Learn how your comment data is processed .

bhavna

most insightful tutorial on this topic! thanks

veeraraghava

how to call deafault method our run method in commandline runner interface as rest controller in requestmapping

zako

thx , just this method doesnt works on my eclipse , final JasperReport report = loadTemplate();

Koukou

Comment ajouter le fichier. Jrxml dans sts, mon sts ne lut pas le fichier

Mohamed BELMAHI

The Render pdf (JasperReportsUtils) with Spring framework 5 doesn’t work more. How can I change ?

Avneesh Saini

You are directly use JasperFillManager and JasperExportManager as shared below:

final JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource()); JasperExportManager.exportReportToPdfFile(jasperPrint, pdfFile.getAbsolutePath());

Kanagalingam J

How to load custom font in Jasper ?

xyz

You may use iReport or Jasper studio and install the fonts and generate their extensions and use them in your project.

deepak

I got this error

net.sf.jasperreports.engine.util.JRFontNotFoundException: Font “Arial Rounded MT Bold” is not available to the JVM. See the Javadoc for more details. at net.sf.jasperreports.engine.fonts.FontUtil.checkAwtFont(FontUtil.java:604) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.loadFont(SimpleTextLineWrapper.java:384) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.getGeneralFontInfo(SimpleTextLineWrapper.java:354) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.createFontInfo(SimpleTextLineWrapper.java:294) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.start(SimpleTextLineWrapper.java:256) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.TextMeasurer.measure(TextMeasurer.java:543) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRFillTextElement.chopTextElement(JRFillTextElement.java:665) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRFillTextField.prepare(JRFillTextField.java:784) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:542) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRFillFrame.prepare(JRFillFrame.java:241) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:542) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:453) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:428) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillBandNoOverflow(JRVerticalFiller.java:448) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnHeader(JRVerticalFiller.java:496) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:260) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:110) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:615) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:432) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:818) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:61) ~[jasperreports-6.6.0.jar:6.6.0] at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:221) ~[jasperreports-6.6.0.jar:6.6.0] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_191] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_191] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]

Valsaraj Viswanathan

Download and place the font in jre/lib/fonts folder to fix.

Eqbal

How to attach new Jasper report without modifying Spring Boot application to call new jasper repot. I mean to make the Report dynamic ,

Recent Posts

  • Upgrade Log4j version on Sap Hybris Commerce to fix vulnerability
  • Difference between Impex decorator vs translator in Hybris
  • Tanuki Wrapper and SAP Hybris
  • JDK-14 Exception Details Feature : Add More Details To Your NullPointerException
  • json-server : Mock DB + API Server for testing and development
  • Files customization in SAP Hybris (Add dependency to platform + Customize setantenv.sh)

You must be logged in to post a comment.

How to generate PDF File using Jasper Reports

Step 1: import the jasper-report-connector to project ​.

  • Download the latest jasper-report-connector zip from here
  • Import the downloaded jasper-report-connector zip into your project using the Import Resource option to the Connector folder.

Step 2: Create a Java Service ​

  • Create a Java Service , named JasperReportService
  • Add import statement for jasper-report-connector interface. import com.wavemaker.connector.jasper.JasperConnector;
  • Autowire the jasper report service @Autowired private JasperConnector jasperConnector;

We can generate the Jasper Reports using various data sources in WaveMaker projects. This document will present the data base and Api data as the datasources.

Example 1: Generating Jasper Report from a imported DataBase: ​

Build your report using Jasper Studio , which enables you to build and customize look and feel of the report. Once you design the report export the reportxml(.jrxml) and import into WaveMaker project using Import Resource to the ${ServiceName}/src/ folder

The following sample report jrxml is provided for your reference which loads the data from hrdb Employee table and generates the report.

jasper report to pdf java

Import Sample HRDB database into the project

Autowire the imported DataBase dataSource bean in to the created java serice

Here Qualifier name is ${databaseServiceName}DataSource

Java service code to generate Jasper Report PDF from hrdb Employee table

Example 2: Generating Jasper Report by consuming the API Data ​

To generate PDF file using Jasper Reports, data needs to be consumed from an external REST API. This data should be passed to the jasper report to genereate the PDF file.

Steps to generate PDF file using Jasper reports are as follows:

Take a Random User Rest api .

The Rest api response JSON output looks typically as follows:

Design a jasper report for the above data. The following is a sample report file. Which we need to import into the project using Import Resource to the ${Service}/src/ folder.

Create a method named generatePDFReport and call the jasper-report connector appropriate method, which will return the generated PDF Jasper Report.

Step 3: In the Main page drag and drop an Iframe onto the canvas. ​

jasper report to pdf java

  • Save and run the project – The PDF Report gets generated. You can download the PDF file.
  • PDF report for Employee data

jasper report to pdf java

  • PDF report for Rest API data

jasper report to pdf java

  • Step 1: Import the jasper-report-connector to project
  • Example 1: Generating Jasper Report from a imported DataBase:
  • Example 2: Generating Jasper Report by consuming the API Data
  • Step 3: In the Main page drag and drop an Iframe onto the canvas.

Jaspersoft Community

  • Remember me Not recommended on shared computers

Forgot your password?

  • Join the Community

Exporting .jasper to .pdf with Java

  • Jaspersoft Studio

By wim040 May 31, 2023 in Products

Recommended Posts

New to Jaspersoft and Java, but I understand the basics. As the titles sugested, I want to export my .jasper while to .pdf, but i keep getting error messages and my pdf file won't open.

Does anyone know, why the pdf export is not succesfull? Let me know if you need more information!

Greetings, w.

Link to comment

Share on other sites.

  • Created May 31
  • Last Reply Jun 1

Top Posters In This Topic

Mehak Rajkumar

Popular Days

wim040 2 posts

lucianc 1 post

Mehak Rajkumar 1 post

May 31 2023

Try to do outputStream.close(); after exporter.exportReport();

@lucianc Thanks for the response! But sadly it does not work, I keep getting the following error messages: jdbc:mysql://127.0.0.1:3306/willem_bi Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/pdf/FontMapper         at net.sf.jasperreports.export.pdf.classic.ClassicPdfProducerFactory.createProducer(ClassicPdfProducerFactory.java:44)         at net.sf.jasperreports.engine.export.JRPdfExporter.createPdfProducer(JRPdfExporter.java:902)         at net.sf.jasperreports.engine.export.JRPdfExporter.initExport(JRPdfExporter.java:733)         at net.sf.jasperreports.engine.export.JRPdfExporter.exportReport(JRPdfExporter.java:694)         at JasperReportHelloWorld.main(JasperReportHelloWorld.java:67) Caused by: java.lang.ClassNotFoundException: com.lowagie.text.pdf.FontMapper         at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)         at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)         at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)         ... 5 more

Greeings, Willem

Mehak Rajkumar

Thank you for posting to the Jaspersoft Community. Our team of experts has read your question and we are working to get you an answer as quickly as we can. If you have a Jaspersoft Professional Subscription plan, please visit https://support.tibco.com/s/ for direct access to our technical support teams offering guaranteed response times.  

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Already have an account? Sign in here.

  • Knowledge Base
  • Documentation
  • All Activity
  • Create New...

© 2024 Cloud Software Group , Inc. All rights reserved.

  • Terms of Use
  • Privacy Policy
  • Cookie preferences
  • Jaspersoft commercial edition
  • Jaspersoft community edition
  • Download Jaspersoft
  • Jaspersoft Cloud BI

404 Not found

IMAGES

  1. Generate Simple PDF Report using Jasper Report and Java

    jasper report to pdf java

  2. Jasper Reports Step by Step in Java Netbeans

    jasper report to pdf java

  3. How to create a jasper pdf report from a json datasource in Java?

    jasper report to pdf java

  4. java

    jasper report to pdf java

  5. Using Jasper Reports to create reports in Java

    jasper report to pdf java

  6. Jasper Reports 【JavaでのPDFの作成方法】

    jasper report to pdf java

VIDEO

  1. 00# Demo: Full Stack Project in Java + Oracle + Jasper Report

  2. 46# Java + Oracle + Jasper Report: Display those employees who are working in a specific City

  3. Jasper Reporting

  4. Part 2

  5. Summary Band

  6. 38# Java + Oracle + Jasper Report: Download Jasper Report Library and iReport plugin for NetBeans

COMMENTS

  1. java

    java - Export JasperReport to PDF OutputStream? - Stack Overflow Export JasperReport to PDF OutputStream? Ask Question Asked 12 years, 1 month ago Modified 4 years, 8 months ago Viewed 101k times 34 I'm writing a pretty simple sample project for familiarizing myself with Jasper Reports.

  2. Generate PDF File from Java using Jasper Reports Library

    Generate a PDF File with JasperReports and Java This tutorial will explain you how to Generate a PDF File from Java using Jasper Reports Library. Generate a PDF File with JasperReports and Java Creating a Jasper Report (using grouping or not) is not a difficult task (for this we can use Jaspersoft Studio).

  3. Generate PDF document using JasperReports and Spring boot

    2.1. JasperReports Template. First of all, we need to create the JasperReports template of our invoice PDF document. 1. Download and install the Jaspersoft Studio. 2. Open the Jaspersoft Studio and create a JasperReports template using the toolbox that comes with it. I have already generate a JasperReports template, you can find it in Github.

  4. Create PDF using Jasper Report Java

    Mar 15, 2022 — by Coder Sathi in How to, Java In this post, we will learn to create PDF using JasperReport. Let's take a simple example. We have a list of cricketers' data and want to add these data in our PDF file. Let's assume that we already have a jrxml file created.

  5. JasperReports Library

    Product: JasperReports® Library Followers In order to fill a report-design, one can use the fillReportXXX () methods exposed by the JasperFillManager class.

  6. JasperReports with Spring

    1. Overview JasperReports is an open source reporting library that enables users to create pixel-perfect reports that can be printed or exported in many formats including PDF, HTML, and XLS. In this article, we'll explore its key features and classes, and implement examples to showcase its capabilities. 2. Maven Dependency

  7. How to Generate PDF with Java

    Welcome to the 7th part of our comprehensive Jasper Reports tutorial series! In this tutorial, we'll walk you through the step-by-step process of generating ...

  8. How to generate PDF File using Jasper Reports

    Download the latest jasper-report-connector zip from here; Import the downloaded jasper-report-connector zip into your project using the Import Resource option to the Connector folder. Step 2: Create a Java Service Create a Java Service, named JasperReportService; Add import statement for jasper-report-connector interface.

  9. Jasper Report Library, PDF Report and Java

    Jasper Report Library, PDF Report and Java Rajendra Kumar Yadav, M.Sc (CS) · Follow 5 min read · Dec 17, 2019 I am using Eclipse with Jasper Report Plugin and Maven for Automation...

  10. Run Jasper report and export as pdf from Java Swing application

    Run Jasper report and export as pdf from Java Swing application. I have made a Java Swing application which produces an xml file with data from a mysql database. Using that I have successfully made a Jasper (jrxml) report using the Jasper 5.5.0 plugin for NetBeans separately from my Swing application. To do this I use the prepared xml file as ...

  11. java

    1 Answer Sorted by: 1 You can use a JRPdfExporter and then pass your JasperPrint variable to the setExporterInput. the bellow code gives more details :

  12. Exporting .jasper to .pdf with Java

    Thank you for posting to the Jaspersoft Community. Our team of experts has read your question and we are working to get you an answer as quickly as we can.

  13. Creating a Jasper Report with Spring Boot

    JasperReports is a Java reporting tool that writes content to the printer, onto the screen, or into files like PDF, XLS, RTF, CSV, XML, HTML, ODT, and TXT. Combined with Spring Boot, we can create APIs allowing users to download the application data in the form of reports.

  14. Generate PDF File from Java using Jasper Reports Library

    Cause a PDF File with JasperReports and Java This tutorial will explain thou how to Generate a PDF File from Java using Jasper Berichterstattungen Library. Generate a PDF File with JasperReports and Caffeine Creating a Jasper Account (using grouping alternatively not) exists not a difficult task (for this we can use Jaspersoft Studio).

  15. Jasper Reports with Spring Boot Example

    Jasper Reports with Spring Boot Example | Design & Export PDF reportsIn this video you will learn 1. How to Export Data To PDF using Spring Boot, JasperRepor...

  16. JRPdfExporter (JasperReports 6.21.0 API)

    PDF viewer applications are able to execute Acrobat JavaScript code that is embedded in the PDF and associated with different events. JasperReports only allows inserting Acrobat JavaScript code. This code gets executed when the PDF document is opened in the viewer. This can be achieved using the getPdfJavaScript () configuration setting, which ...

  17. java

    1 Answer Sorted by: 4 I'd like it go to a printer as a PDF. I don't care about ever seeing it on screen, straight to printer would be the ideal You can't do it with plain HTML/CSS/JS. As JSF is basically just a HTML/CSS/JS code generator, it can't do any magic for you.

  18. Reporting in Java using DynamicReports and JasperReports

    3.1 Create a new empty report object. JasperReportBuilder report = DynamicReports.report (); 3.2 Now create a report colum for each database column. Columns.column ( "Customer Id", "id", DataTypes.integerType ()) The first parameter is a column label that will be shown in the report header.

  19. java

    1 Answer Sorted by: 0 Well I kind of found a solution... First you write the JasperPrint object to disk and then you can stream the content and create PDF via streams. No memory problems anymore but you need to save the result first on the disk.

  20. Print from Table to PDF Excel Word using Dynamic Jasper Report in Java

    Java Dynamic Jasper ReportThis video I will show how to print report from jtable to pdf, excel, work and image file by using dynamic jasper report. and print...

  21. Exporting a JasperReport to PDF, Characters Missing

    It will create as many as three JasperPrints from a single report: one prints on the printer, one is serialized and saved to the database, and the third is exported to PDF using Jasper's built-in export capability.