PDA

View Full Version : JFreeReport Servlet...



Anonymous
07-09-2002, 05:21 PM
Hi, I think I can provide the servlet sample code, but I need some help:

Can someone provide me a JFreeReport HelloWorld?. The included demo is too big as to extract some simple code and test it on a servlet. I already know how to send a PDF from a servlet using IText (the PDF library used by JFreeReports).

I think that these are the basic steps to produce a report from a servlet:

1) get a report object given the XML file and the datasource

URL in = getClass ().getResource (urlname);
ReportGenerator gen = ReportGenerator.getInstance ();
JFreeReport report1 = null;
report1 = gen.parseReport (in, in);
report1.setData (data);

2) send the report to the PDF Target:

PrinterJob pj = PrinterJob.getPrinterJob ();
PageFormat pf = pj.validatePage (???reportPane.getPageFormat ()???);

ByteArrayOutputStream b = new ByteArrayOutputStream ();
PDFOutputTarget target = new PDFOutputTarget (b, pf, true);
target.open ("Title", "Author");
report1.processReport (target);
target.close ();

Q: How can I obtain the PageFormat without a reportPane?

3) send the pdf stream to the servlet

res.setContentType("application/pdf");
res.setContentLength(b.size());
ServletOutputStream out = res.getOutputStream();
out.write( b.toByteArray() );
out.flush();
out.close();

Any help will be very appreciated.

Best Regards,
Martin

Anonymous
07-09-2002, 07:04 PM
Hi,

you can get the page format using for instance

Paper paper = new Paper ();
paper.setSize (595.275590551181d, 419.5275590551181);
paper.setImageableArea (70.86614173228338, 70.86614173228347, 453.54330708661416, 277.8236220472441);

PageFormat format = new PageFormat ();
format.setOrientation (PageFormat.LANDSCAPE);
format.setPaper (paper);

Adjust the sizes as you need, the correct values can either be calculated (arrghh, what a cruel way) or experimental extracted using

PrinterJob pj = PrinterJob.getPrinterJob ();
PageFormat fmt = pj.pageDialog (pj.defaultPage ());
System.out.println (fmt.getWidth () + " " + fmt.getHeight ());
System.out.println (fmt.getImageableX () + " " + fmt.getImageableY () + " " + fmt.getImageableWidth () + " " + fmt.getImageableHeight ());

to show the page setup dialog and to display the selected values.
You don't need to aquire a printjob, just feed the created PageFormat into the outputtarget, and everything will be fine.

The code lines are taken from the ReportPropertyLostBug.java testcase located in the "JfreeReport-ext" project in package "com.jrefinery.report.ext.junit.bugs".

Have more fun,
said Thomas


Have more fun,
said Thomas