Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Js|pdf| detailed before a long time to do a PDF report through JSP Small project, is an open vision. Some of the enterprise information through the network to form an HTML report, although IE can directly print the contents of the display, but from the interface, if the direct HTML display results printed out, it is not very beautiful. If you turn it into a PDF file and then print it, the printing effect will be much better.
1, Itext introduction
Itext is an open source Java class Library that can be used to easily generate PDF files. You download the latest version of the class library by visiting http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948, After the download is complete, you will get a. jar package, which can be used by adding this package to the JDK classpath.
If you need to see Chinese, Japanese, and Korean characters in the generated PDF file, you also need to download the Itextasian.jar package by accessing Http://itext.sourceforge.net/downloads/iTextAsian.jar.
Http://www.lowagie.com/iText/tutorial/index.html has a more detailed tutorial on the use of the IText class library. This tutorial begins with a more systematic introduction to the methods and techniques of putting text, pictures, forms, and so on in a PDF file.
After reading this tutorial, you can basically do some simple to complex PDF files. However, trying to solve all the difficulties in the process of generating PDF files through tutorials is an extravagant hope. Therefore, it is important to read the Itext API documentation. While downloading the class library, the reader can also download the document for the class library.
2. How to use Itext to generate PDF reports in Java programs
The following is one of the simplest examples of the above tutorial, which depicts the general procedural framework for generating PDF files through Itext. Readers only need to add the content they want to put in the PDF file in the middle of Document.open () and Document.close () two statements. This example only adds the word "Hello World" to the PDF file.
As can be seen from the above examples, the framework of the procedure is very clear. However, it is very troublesome to specify the position of text, drawing, and table in the PDF. There seems to be no better way to do this than to constantly modify the location in the program, then run the program, generate a PDF file, and see if the elements are reasonable in the PDF.
3. How to generate PDF report through JSP
This part is not in the Itext tutorial, the relevant information on the Internet is also relatively few. After a while, I found that the PDF file is generated on the server, and then the user chooses to download or open it by clicking the hyperlink to the PDF file. It's a thought, or one of the ideas. This paper realizes this idea, and gives another idea and realizes it through two ways.
1 The PDF file is generated directly on the server.
The above program generates a static PDF file on the server. Obviously, the name of the PDF file to run each time should be unique and not heavy. This program uses random functions to name the generated PDF file. The disadvantage of this procedure is that each run will produce a PDF file on the server, if not deleted in time, the number will become larger and bigger, this is obviously the site defenders do not want to see.
2 Transfer the PDF file to the client's cache via streaming form. The advantage of this is that there will be no "relics" left on the server.
i) directly through the JSP page generation
Page import= "java.io.*,
Document document = new document ();
= new Bytearrayoutputstream ();
Pdfwriter.getinstance (document, buffer);
Document.add (New Paragraph ("Hello World"));
DataOutput output =
byte bytes = Buffer.tobytearray ();
for (int i = 0;
i < bytes.length;
I used the second method in the project. This article's source code in my tomcat4 above all is debugging passes. Hope can bring convenience to everyone.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
and provide relevant evidence. A staff member will contact you within 5 working days.