Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 13th, 2006, 04:15 PM
Registered User
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to PDF using Jasper Report in JAVA

I am doing a project to generate report from XML file to PDF file using Jasper in JAVA. But I am not familiar with Jasper as well as XML.
I need some help....


Old February 9th, 2006, 02:24 AM
Registered User
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

Hi ,
What ur problem?where did u stuck?

Did u worked any sample example

Old February 21st, 2006, 03:24 AM
Authorized User
Join Date: Aug 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts

Hi ,

  Here i am sending some sample report generated in PDF format using jasper

1)save this file as stcokroom.jrxml in u r c drive.
2) change the database settings and query ...u will get


This Is My File (stockroom.jrxml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="DataSourceReport" pageWidth="595" pageHeight="842" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50">
<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Bold" isDefault="false" fontName="Arial" fontSize="12" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Italic" isDefault="false" fontName="Arial" fontSize="12" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>

<field name="STOCKROOM_ID" class="java.lang.Integer">
<field name="STOCKROOM_NAME" class="java.lang.String">
<field name="STOCKROOM_DESC" class="java.lang.String">

<band height="20">

<reportElement mode="Opaque" x="0" y="5" width="55" height="15" forecolor="#ffffff" backcolor="#333333" style="Arial_Bold"/>
<textElement textAlignment="Center"/>
<reportElement mode="Opaque" x="55" y="5" width="205" height="15" forecolor="#ffffff" backcolor="#333333" style="Arial_Bold"/>
<reportElement mode="Opaque" x="260" y="5" width="255" height="15" forecolor="#ffffff" backcolor="#333333" style="Arial_Bold"/>


<band height="15">
<textField bookmarkLevel="2">
<reportElement x="0" y="0" width="50" height="15"/>
<box leftBorder="Thin" bottomBorder="Thin" leftPadding="10" rightPadding="10"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{STOCKROOM_ID}]]></textFieldExpression>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="50" y="0" width="200" height="15"/>
<box leftBorder="Thin" bottomBorder="Thin" leftPadding="10" rightPadding="10"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{STOCKROOM_NAME}]]></textFieldExpression>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="250" y="0" width="265" height="15"/>
<box leftBorder="Thin" bottomBorder="Thin" rightBorder="Thin" leftPadding="10" rightPadding="10"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{STOCKROOM_DESC}]]></textFieldExpression>



And This My Java Jasper Program for PDF:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class JasperReportDemo
public static void main(String[] args) throws SQLException, FileNotFoundException,
IllegalAccessException, ClassNotFoundException,Exception


final String driverClass = "xxxxxxxxxxxxxxxxxx";
final String connectionURL = "xxxxxxxxxxxx ";
final String userID = "xxxxxxxxxxx";
final String userPassword = "zzzzzzzzzzzz";
Connection con = null;
Statement stmt = null;
ResultSet rset = null;
con = DriverManager.getConnection(connectionURL, userID, userPassword);
+ "FROM stockroom ";
stmt = con.createStatement();
rset = stmt.executeQuery(queryString);
InputStream input = new FileInputStream(new File("c:/stockroom.xml"));
JasperDesign design = JRXmlLoader.load(input);
JasperReport report = JasperCompileManager.compileReport(design);
JRResultSetDataSource jasperReports = new JRResultSetDataSource(rset);
JasperPrint print = JasperFillManager.fillReport(report, new HashMap(), jasperReports);
long start = System.currentTimeMillis();

OutputStream output = new FileOutputStream(new File("c:/output/JasperReport.pdf"));
JasperExportManager.exportReportToPdfStream(print, output);

Old October 12th, 2006, 05:41 AM
Registered User
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shashideore

Hi raj_plkd,
I'm using the code you have provided here to export to pdf .It is working fine.
But i get an error "WARNING: The supplied java.sql.Connection object is null"
if i use a subreport in my main report.

Can you please help me resolve this problem.

Thanks in advance.

Shashikant Deore
Old November 23rd, 2006, 12:42 AM
Registered User
Join Date: Aug 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

why are u struggling lot , use i report for report generation and in action u can compile jasper report to pdf easily by 2 line code... try it

Old November 24th, 2006, 07:59 AM
Authorized User
Join Date: Sep 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts

connection object null means check man whether connection object is null or not


Old July 5th, 2007, 04:44 AM
Registered User
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

[Hi ,
this is sathiya. i have to generate jasperreport for app and i have xml form of input data, i need to convert xml into pdf format.

 i don't know how to compile the jasper program. is there any .jar file to compile that.

i need help. if any one aware of jasper process. please let me know for this mail id: [email protected]

thanks and regards,

Old December 7th, 2011, 01:43 AM
Registered User
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile vijji

i dont have knowledge regarding jasper reports can u send me some tutorials regarding to jasper reports.

Similar Threads
Thread Thread Starter Forum Replies Last Post
jasper to pdf maktwin J2EE 1 March 11th, 2009 02:33 PM
jasper report with netbeans 5.0 gita_Sathyam Java Basics 0 April 14th, 2008 04:34 AM
Problems While generating pdf using Jasper kiran_p2p J2EE 0 December 27th, 2006 12:04 AM
how to run jasper report from java file? hiraal_thoria J2EE 1 March 14th, 2005 04:41 AM
jasper report [email protected] J2EE 0 February 3rd, 2005 06:48 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.