• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Jasper Report - Text Exporter

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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();


Thanks in advance,
Yaddif
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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
 
Yaddif Medina
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

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.

I am a little frustrated for this .
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


CHAR_WIDTH = REPORT_WIDTH / MAX_CHAR_PER_ROW
CHAR_HEIGHT = REPORT_HEIGHT / MAX_CHAR_PER_COL

CHAR_WIDTH = 524 / 80 = 6.55
CHAR_HEIGHT = 524 / 44 = 11.9


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());

return result;
}

/**
* Transforms x coordinates from pixel space to character space.
*/
protected int calculateXCoord(int x)
{

int result = Math.round((float)pageWidth * x / jasperPrint.getPageWidth());

return result;

}















 
I was born with webbed fish toes. This tiny ad is my only friend:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic