Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exporting variable values to pdf 1

Status
Not open for further replies.

briggsy79

Programmer
Feb 23, 2001
68
SE
Hi,
I was hoping to create a program that would allow users to click lots of radio buttons, check boxes, select from combo boxes..etc. Then all this information could be exported to a report hopefully in the pdf format(Just because then they wouldn't be able to edit it, and it would be platform independant?).
I've looked around and found jasperreports and iReport aswell as PJx. But jasperreports seems to rely on some form of database connection, and i doubt PJx will be able to create nice looking forms.
I dont want to create a database connection to work with jasper because it just seems unnecessary, that is; all the info could quite easily be stored as variables at run time and then just passed to the report, none of it needs to be stored for later reference.

I know everyone thinks this about their proograms, but it just seemed like the kind of project that someone would have done before, or at least have a few third party controls out there to make it easy.

Am I missing something, or is this going to be an arduous task? Are there some third party packages that would do just what I need?
To be honest any information or opinion would be greatly appreciated.

Thanks,
David
 
Well, I only had a quick look at JasperReports, but from what I can tell, JDBC capability is not required by any stretch of the imagination. It appears it is quite possible to create a report from an xml file ...

--------------------------------------------------
Free Database Connection Pooling Software
 
This is going to sound stupid but...
How would I go about this? I've obviously missed something and I have no experience with XML so an example or two would be extremely helpful.
Thanks.
 
Ok I've read through all of the 'how to' guides, the API, as well as the faq and I still cant see how to do it.
Is there some piece of information that I'm missing? As I said I have no xml experience, so some of that may go over my head. But still I would imagine I would have been able to work it out. (maybe my recent HD for software development has given me a false sense of security :p )

Would this be easier with another program, crystral reports for instance?

In case this helps this is the project outline. Its for a building inspection company. Their inspectors would go around looking at the property and ticking and setting options on my program as they walked around (with a laptop). At the end of the inspection a CD could be presented to the house owners with a locked pdf file which listed the results of the inspection. Sounded easy in my head...

Thanks again.

-David
 
I know nothing about jasper, but if you feel unconfortable with it, you can always generate the report on another format(txt, xml, ...) and the translate it to pdf.

Cheers.

Dian
 
So..
I use some visual XML designer to create the desired structure.
Then paste this code in my project and just use the variables where I need to.
Finally I export it somehow to a pdf.
Is this the idea?
If so, any tips on a visual XML designer to create a good report structure? And also do I just need to use something from the adobe SDK to export 'code' to a pdf?
 
Well I had a quick look into JasperReports, and I think it is seriously impressive - and as I said before, you do not need to use a database (although it is supported, and would be very useful for fully dynamic reports).

To generate a PDF, you need to download the jasperreport.jar from the website, and also download iText.jar (google for iText download - its at lowagie.com).

A simple example is this :

Code :
Code:
import net.sf.jasperreports.view.*;
import net.sf.jasperreports.engine.*;
import java.util.*;
import java.io.*;

public class TestJasperReports {
	public static void main(String[] args) throws Exception {
		// compile the xml
		JasperReport jr = JasperCompileManager.compileReport("test_jasper_report.xml");

		// generate the pdf as a byte[] array
		byte[] report = JasperRunManager.runReportToPdf(jr, null, new JREmptyDataSource());

		// write the file
		FileOutputStream fos = new FileOutputStream("test_jasper_report.pdf");
		fos.write(report);
		fos.flush();
		fos.close();

	}
}

Input file (test_jasper_report.xml) :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created using JasperAssistant ([URL unfurl="true"]http://www.jasperassistant.com)[/URL] -->
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "[URL unfurl="true"]http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">[/URL]

<jasperReport name="FontsReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">
	<title>
		<band height="782">
		
			<staticText>
				<reportElement x="0" y="50" width="150" height="40"/>
				<textElement/>
				<text><![CDATA[The quick brown fox jumps over the lazy dog.]]></text>
			</staticText>
			<staticText>
				<reportElement x="160" y="50" width="390" height="40"/>
				<textElement/>
				<text><![CDATA[<staticText><reportElement x="0" y="50" width="150" height="40"/><text>The quick brown fox jumps over the lazy dog.</text></staticText>]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="100" width="150" height="40"/>
				<textElement>
					<font size="12"/>
				</textElement>
				<text><![CDATA[The quick brown fox jumps over the lazy dog.]]></text>
			</staticText>
			<staticText>
				<reportElement x="160" y="100" width="390" height="40"/>
				<textElement/>
				<text><![CDATA[<staticText><reportElement x="0" y="100" width="150" height="40"/><textElement><font size="14"/></textElement><text>The quick brown fox jumps over the lazy dog.</text></staticText>]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="150" width="150" height="40"/>
				<textElement>
					<font fontName="serif" size="12" pdfFontName="Times-Roman"/>
				</textElement>
				<text><![CDATA[The quick brown fox jumps over the lazy dog.]]></text>
			</staticText>
			<staticText>
				<reportElement x="160" y="150" width="390" height="40"/>
				<textElement/>
				<text><![CDATA[<staticText><reportElement x="0" y="150" width="150" height="40"/><textElement><font fontName="serif" size="12" pdfFontName="Times-Roman"/></textElement><text>The quick brown fox jumps over the lazy dog.</text></staticText>]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="200" width="150" height="40"/>
				<textElement>
					<font fontName="serif" size="12" isBold="true" isItalic="true" pdfFontName="Times-BoldItalic"/>
				</textElement>
				<text><![CDATA[The quick brown fox jumps over the lazy dog.]]></text>
			</staticText>
			<staticText>
				<reportElement x="160" y="200" width="390" height="40"/>
				<textElement/>
				<text><![CDATA[<staticText><reportElement x="0" y="200" width="150" height="40"/><textElement><font fontName="serif" size="12" isBold="true" isItalic="true" pdfFontName="Times-BoldItalic"/></textElement><text>The quick brown fox jumps over the lazy dog.</text></staticText>]]></text>
			</staticText>
			<staticText>
				<reportElement x="160" y="300" width="390" height="40"/>
				<textElement/>
				<text><![CDATA[<staticText><reportElement x="0" y="300" width="150" height="40"/><textElement><font fontName="Comic Sans MS" size="12" isBold="true" pdfFontName="COMICBD.TTF"/></textElement><text>The quick brown fox jumps over the lazy dog.</text></staticText>]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="350" width="150" height="40"/>
				<textElement>
					<font fontName="monospaced" size="12" isItalic="true" isUnderline="true" pdfFontName="Courier-Oblique"/>
				</textElement>
				<text><![CDATA[The quick brown fox jumps over the lazy dog.]]></text>
			</staticText>
			<staticText>
				<reportElement x="160" y="350" width="390" height="40"/>
				<textElement/>
				<text><![CDATA[<staticText><reportElement x="0" y="350" width="150" height="40"/><textElement><font fontName="monospaced" size="12" isItalic="true" isUnderline="true" pdfFontName="Courier-Oblique"/></textElement><text>The quick brown fox jumps over the lazy dog.</text></staticText>]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="400" width="150" height="40"/>
				<textElement>
					<font fontName="monospaced" size="12" isBold="true" isStrikeThrough="true" pdfFontName="Courier-Bold"/>
				</textElement>
				<text><![CDATA[The quick brown fox jumps over the lazy dog.]]></text>
			</staticText>
			<staticText>
				<reportElement x="160" y="400" width="390" height="40"/>
				<textElement/>
				<text><![CDATA[<staticText><reportElement x="0" y="400" width="150" height="40"/><textElement><font fontName="monospaced" size="12" isBold="true" isStrikeThrough="true" pdfFontName="Courier-Bold"/></textElement><text>The quick brown fox jumps over the lazy dog.</text></staticText>]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="450" width="150" height="40" forecolor="#ff0000"/>
				<textElement>
					<font size="14"/>
				</textElement>
				<text><![CDATA[The quick brown fox jumps over the lazy dog.]]></text>
			</staticText>
			<staticText>
				<reportElement x="160" y="450" width="390" height="40"/>
				<textElement/>
				<text><![CDATA[<staticText><reportElement x="0" y="450" width="150" height="40" forecolor="red"/><textElement><font size="14"/></textElement><text>The quick brown fox jumps over the lazy dog.</text></staticText>]]></text>
			</staticText>
		</band>
	</title>
</jasperReport>

--------------------------------------------------
Free Database Connection Pooling Software
 
Thank you so much, this is exactly what i needed.
 
ok, this is getting frustrating (for everyone i'm sure)

I'm using JBuilder, and I have included iText and jasper in my projects libraries, but now when i go to run the app i get this error:
Code:
java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
	at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:186)
	at jasperreporttest.JasperTest.main(JasperTest.java:22)
Exception in thread "main"

This occurs at:
Code:
JasperReport jr = JasperCompileManager.compileReport("test_jasper_report.xml");

What am I doing wrong here?
Is there something i've missed (well obviously)
Any ideas?

Thanks.
 
You need a few Apache commons libraries aswell. Have you got tomcat ? As they all come with that. Failing that, you'll need to download them from thee Apache Commons project site ...

You need :

commons-beanutils.jar
commons-digester.jar
commons-collections.jar

I *think* thats it ...

--------------------------------------------------
Free Database Connection Pooling Software
 
hehe, I couldn't find where the libraries were after installing tomcat (uni really doesnt prepare you for using 3rd party classes, damn learning!), so I included my entire JBuilder folder...it worked.

Thanks so much sedj, you have been a terrific help.
 
This is where they are :

%TOMCAT_HOME%\server\lib\commons-beanutils.jar
%TOMCAT_HOME%\server\lib\commons-digester.jar
%TOMCAT_HOME%\common\lib\commons-collections.jar

I generally find that uni grads who did comp. sci. think "Oh yeah, I know how to programme" - but its not as simple as writing a "book shop" or some other simple application. And they don't teach you how to upgrade an API version, only to find half of your code that "used to work" doesn't any more !!!

Good luck in the future & job interview :)

--------------------------------------------------
Free Database Connection Pooling Software
 
iv heard about jasper reports but dont know much about it. Iv been trying to figure out if it allowes yuo to edit pdf files through java code (i.e. take out certain pages or delete content) all throuhg the API.

any ideas??
 
I'm fairly sure that you cannot do this through JasperReports, because it is not a PDF manipulation API - it just uses iText to render the content to PDFs.
Your best bet is to look at the iText API.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top