This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am working with JasperReport in my web application (generating pdf files) but now my client wants a Text file output. I have tried with the JRTextExporter but it generates just 12 blank lines in the txt file .. (I have tried with the rtf exporter and it gives me a blank file too)!!!.
I am using the jasperreports-1.2.4.jar
PLEASE HELP ME, because I am getting upset for this.
My code is: JRTextExporter exporter = new JRTextExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "c:\\borra1.txt"); exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(200)); exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(50)); exporter.exportReport();
I ahev worked with Jasper Reports for a long while now and in my opinion its a bit tedious to say the least! :-) Anyways i have moved over to BIRT ( an eclipse project ) it uses Jasper Reports in the back but they seem to have taken the complexity and tediousness out :-)
Well to your solution, what i did is this:
basically just writing to the servletoutput stream which prompts the user to download a file, it works well, let me know ho wyou get on Rich
<a href="http://www.johnboy.co.za" target="_blank" rel="nofollow">http://www.johnboy.co.za</a><br />Change - When the winds of change blow hard enough, the most trivial things can turn into deadly projectiles.
Joined: Oct 05, 2005
I have tried with that way too and nothing change ... I think that the problem is with the Tomcat, because I have installed the iReport and it can export my reports to TXT .... but the Tomcat just send 12 blank lines.
I have copied the whole "lib" directory from iReport to my Tomcat's project and it did not fix it.
JRTextExporter uses a very smart algorithm to convert reports in graphics to reports in text. It first creates a virtual text grid based on your report's size and the character dimensiona given to the exporter. Then it places each individual item to the text grid based on the item's location in pixels.
There are three very important parameters effecting how the exporter converts the output
1- Width and Height of the reports in pixels
2- Widths and Heights of the individual report elements (labels etc.)
3- Character Width and Height parameters of the exporter
To fit the report to the text grid, you need to make a simple calcuation:
First find the maximum number of characters that can be fit to a row when you would print the report in text (Let's assume that it is 80 characters long and call this number MAX_CHAR_PER_ROW).
Then find the maximum number of characters that can be fit to a column when you would print the report in text (Let's assume that it is 44 characters height and call this number MAX_CHAR_PER_COL).
Then Divide your reports dimensions with these numbers to find the character height and width. LEt's assume the dimensions of the report are REPORT_WIDTH and REPORT_HEIGHT and the corresponding values are 524 and 524. Now the characters dimensions are calculated as:
Of course, the resulting values will not be integers most of the time. Then if you want a perfect match, CHAR_WIDTH and CHAR_HEIGHT must be floating numbers. But the original exporter only accepts integers parameters. At this point, use my modified exporter here. I already converted these parameters to float.
Here is how you may send character dimensions to the exporter.
exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Float(6.55));//6.55 //6
exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Float(11.9)); //11//10
Another notice! Make sure that CHAR_HEIGHT is not lower that the actual heights of the report items. Otherwise, the items will not added to the grid since the exporter truncates their heights to 0 because of the arithmetic operation inside the exporter. Another solution would be using rounding instead of truncation by modifying the exporter a little more. Anyway, I did not need that feature since the character height is ok for my case.
* Transforms y coordinates from pixel space to character space.
protected int calculateYCoord(int y)
int result = Math.round((float)pageHeight * y / jasperPrint.getPageHeight());
* Transforms x coordinates from pixel space to character space.
protected int calculateXCoord(int x)
int result = Math.round((float)pageWidth * x / jasperPrint.getPageWidth());