Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save rponte/5044469 to your computer and use it in GitHub Desktop.

Select an option

Save rponte/5044469 to your computer and use it in GitHub Desktop.
Example on how to generate a simple pdf report with JasperReports
package rponte.report;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import rponte.report.ConnectionFactory;
/**
* This article in 'pt_BR' teaches how to generate PDF reports on the Web with JasperReports
* http://blog.triadworks.com.br/jasperreports-gerando-relatorios-pdf-na-web
*
* You'll need these jar's below:
*
* jasperreports-5.0.1.jar
* iText-2.1.7.jar (needed to generate PDF)
* jfreechart-1.0.12.jar (needed to graphics and charts)
* jcommon-1.0.15.jar (needed to graphics and charts)
* commons-beanutils-1.8.2.jar
* commons-collections-3.2.1.jar
* commons-digester-2.1.jar
* commons-logging-1.1.jar
*/
public class GenerateSimplePdfReportWithJasperReports {
public static void main(String[] args) {
Connection connection = null;
try {
String reportName = "myreport";
Map<String, Object> parameters = new HashMap<String, Object>();
connection = new ConnectionFactory().getConnection(); // opens a jdbc connection
// compiles jrxml
JasperCompileManager.compileReportToFile(reportName + ".jrxml");
// fills compiled report with parameters and a connection
JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper", parameters, connection);
// exports report to pdf
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(reportName + ".pdf")); // your output goes here
exporter.exportReport();
} catch (Exception e) {
throw new RuntimeException("It's not possible to generate the pdf report.", e);
} finally {
// it's your responsibility to close the connection, don't forget it!
if (connection != null) {
try { connection.close(); } catch (Exception e) {}
}
}
}
}
@rameramy

rameramy commented Feb 9, 2014

Copy link
Copy Markdown

Hi, the above java code was very useful and worked well to get pdf file. Thanks for sharing here. Keep sharing more and more

@nishikishore

Copy link
Copy Markdown

Hi,
please provide the necessary jar files for executing this source code.
i am unable to import the following package :- rponte.report.ConnectionFactory.
please do the needful as soon as possible.

@justinjamesp1988

Copy link
Copy Markdown

Thanks works fine... !!

@mnisvdn

mnisvdn commented Dec 16, 2015

Copy link
Copy Markdown

Some of the features used here are deprecated, such as JRExporterParameter.OUTPUT_STREAM.
You should update your code.

@anselal

anselal commented Nov 30, 2016

Copy link
Copy Markdown

I agree, some features are deprecated. Can yo update the code to version 6 ?

@yangoff

yangoff commented Jun 7, 2017

Copy link
Copy Markdown

OUTPUT_STREAM Deprecated.
Replaced by SimpleWriterExporterOutput.SimpleWriterExporterOutput(java.io.OutputStream) and SimpleOutputStreamExporterOutput.SimpleOutputStreamExporterOutput(java.io.OutputStream).

@vecadithya

Copy link
Copy Markdown

Hi,
i am unable to import the following package :- rponte.report.ConnectionFactory.
please do the needful as soon as possible.

@NagarajuDevandla

Copy link
Copy Markdown

it is not working ,it give
cvc-complex-type.3.2.2: Attribute 'uuid' is not allowed to appear in element 'jasperReport'.
Exception

@danielleitcs

Copy link
Copy Markdown

Thanks for the code. Would love to add your review of JasperSoft to IT Central Station.

Users interested in solutions that help generate PDF reports also read reviews for Windward. You can see what the IT Central Station community has to say about this tool here.

@zonefame-onelove

Copy link
Copy Markdown

Hi Team,

I can't able to add.

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;

image

any body please help me

@AngryDisk

Copy link
Copy Markdown

@rponte

rponte commented Mar 29, 2019

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment