JasperReport learning essential manual

zhaozj2021-02-16  49

JasperReport learning essential manual

(The younger brother first post, as if you use a small font to look good, everyone will take it)

This is a little experience I learned JasperReport. Welcome everyone to finish, enlighten, I just give a brick role, everyone will then throw jade, I hope I can really accumulate a must-have manual, huh, huh.

Suppose you have a certain understanding of JasperReport (at least a probably developing process), here you don't have to explain the benefits of JASPERREPORT, design, expression definitions, etc. Some things in JasperReport bring your documents.

Maybe because I am a java newbie, I have a lot of problems, only some of them, and attach my solution:

l JSP does not have a problem with PDF

l When generating HTML, JRHTMLEXPORTERPARETER.IMAGES_MAP problem

l Chinese display problem

l Image display implementation (JFreechart)

l My general .jasper generation method

l My project implementation

JSP does not have a problem with PDF

Problem Description: When running the included Web (WebApp Directory) (I use the server is Tomcat), generate PDF with a servlet is completely no problem, and JSP mode is reported to getOutputStream () HAS Already Been Called for this response error, Look at the JSP source code discovery and servlet is almost.

Problem: Tomcat first executes the Java code starting with the .jsp file, then prepare the session, OUT and other variables. The code of the output PDF appears in the <% ...%> segment started by .jsp file, this httpserverletresponse's getOutputStream () method has been called. This method defines this method in the JSP specification can only be called once, so it will inevitably be wrong when Tomcat prepares the OUT variable.

So Tomcat-based servers are wrong, such as JBoss, Sun ONE AppServer

Workaround: Very simple, define the output non-text content according to the JSP / Servlet specification, such as pictures, sound, etc. should use servlet instead of JSP. The output stream in the servlet is completely turned on / off by the application.

Although it seems that this is not a solution, I checked a lot of places, and I didn't find a better statement, so everyone is JasperReport or servlet.

JRHTMLEXPORTERPARETER.IMAGES_MAP problem when generating HTML

This is not a problem, but I am a little confused at the time, I will come out and say it.

I don't know if you haven't read it, you have the following paragraphs each time you generate HTML:

Map imagesmap = new hashmap ();

Session.SetaTRibute ("Images_map", ImageSmap;

Exporter.SetParameter (JrexporterParameter.jasper_Print, Jasperprint);

Exporter.SetParameter (JrexporterParameter.output_Writer, Out);

Exporter.SetParameter (JRHTMLEXPORTERPARETER.IMAGES_MAP, IMAGESMAP);

Exporter.SetParameter (JRHTMLEXPORTERPARETER.IMAGES_URI, IMAGE.JSP? Image = ");

especially:

Exporter.SetParameter (JrexporterParameter.output_Writer, Out); Exporter.SetParameter (JRHTMLEXPORTERPARETER (JRHTMLEXPORTERPARETER.IMAGES_MAP, IMAGESMAP);

Exporter.SetParameter (JRHTMLEXPORTERPARETER.IMAGES_URI, IMAGE.JSP? Image = ");

These words, I feel inexplicable.

Initially guess is to show the picture, can you do it?

And there are a lot of source code for the generated HTML page.

What kind of pictures are it in PX?

View Javadoc, written too simple, not explained at all. So, see the source code:

His.Imagesmap = (map) this.Parameters.Get (JRHTMLEXPORTERPARETER.IMAGES_MAP);

IF (this.imagesmap == null)

{

THIS.IMAGESMAP = New hashMap ();

}

..........

THISLOADIMAGESMAP = new hashmap ();

Byte [] PXBytes = JRIMAGELOADER.LOADIMAGEDATAFROMLOCATION ("DORI / JASPER / ENGINE / IMAGES / PIXEL.GIF");

This.loadedImagesmap.put (PXBytes, "PX");

THIS.IMAGESMAP.PUT ("PX", PXBYTES);

Haha, the original shape is revealed, the original PX is DORI / JASPER / ENGINE / IMAGES / PIXEL.GIF (a small boss), but it has a great role in the beautiful view of the webpage, do not believe in the wrong image (JSP, servlet) path Look (this is also a place where it is wrong).

Chinese display problem

Problem Description: The generated HTML or PDF Chinese is displayed as garbled or not displayed

Solution (HTML):

If you only ask for generating HTML (may not be such a simple customer), it is good if it is jsp: <% @ page contenttype = "text / html; charset = GB2312"%>

If it is servlet: response.setContentType ("text / html; charset = GB2312");

Haha, I'm big, I predict the Chinese solution of PDF, please call the toll consultation telephone 110.

Solution (PDF):

Let's put down the bricks on your hand. I said yet, just don't know if this is a correct or better solution, please give more points.

CHARSET = GB2312 (it is a genius of a stupid) because there is no place when generating PDF.

We have a three steps.

1. Since JasperReport generates PDF using IText, we need IText Asian language packs.

Http://itext.sourceforge.net/downloads/itextasian.jar.

Place him in the LIB.

2. In JasperReport's design XML, you can define a Chinese font:

where pdfFontName = "STSong -Light "pdfencoding =" unigb-ucs2-h "is the font defined in the ITextasian.jar package.

3. Chinese characters use UTF-8 encoding

The Chinese characters saved by Ireport are UTF-8 encoding

You can also use UltraEdit, just menu Advanced - Configuration ... - General checks Auto Detect UTF-8 Files - (ON), you can edit UTF-8 encoded Chinese characters.

Implementation method of picture display (JFreeChart)

The so-called Report, the picture is naturally essential, especially the so-called Chart, in the example of Chart's implementation, in fact, one way to implement, define the chart as Variable, then generate Chart in ChartScriptlet.java (Scriptlet.). SetvariableValue.

In fact, we can see that because JasperReport itself does not take a Chart function, she (please allow me to use "she", who makes me a bachelor programmer, 嘿) CHART is based on other tools, and then as a variable or Parameter passes. In this way, we can make her as much as you want to show any bufferedimage, hey, he is a JFreechart generated or static image.

I am more preferred to define as parameter, because you can get Chart data in the same program that generate JRDataSource, exempt from database operations in the scriptlet, or if it is a simple report, it is saved.

TartDioEngnget.javariter, OUT;

My general .jasper generation method

The examples of JasperReport comes with Ant, but each sample has different build.xml and .class, can you have a unified component program? Ireport can, but he will write all the properties each time, look unclean, clear, so I modified a paradigm build.xm. and .class to build my JasperReport. Interested in the same way can take a look.

1. Definition in building.xml

Dynamic delivery file name

2. Replace all is

3. Modify the program, pass JREMPTYDATASOURCE to each time.

Each time build, just add the parameters on the basis of the previous basis:

-DFILENAME = ******

Such as: ant compile -dfilename = "MyDemo"

Implementation of my project

Just perform a simple package, it may not apply to other systems. If necessary, please contact me. (This time is more rush, etc. have time to make up)

I have to contact information:

QQ: 20469438

Email: avidmouse@qianlong.com

转载请注明原文地址:https://www.9cbs.com/read-22189.html

New Post(0)