Explain how to use Itext to generate PDF reports in the JSP

Source: Internet
Author: User
Tags getmessage net
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.

Document document = new document ();
Try
{
Pdfwriter.getinstance
(Document, New FileOutputStream
("Chap0101.pdf"));
Document.open ();
Document.add (New Paragraph ("Hello World"));
}
catch (Documentexception de)
{
System.err.println (De.getmessage ());
}
catch (IOException IoE)
{
System.err.println (Ioe.getmessage ());
}
Document.close ();

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.

<%@ page import = "com.lowagie.text.*
, com.lowagie.text.pdf.*, java.io.* "%>
<%
String filename =
"PDF" + (new Random ()). Nextint () + ". pdf";
Document document =
New Document (PAGESIZE.A4);
Servletoutputstream OUT1
= Response.getoutputstream ();
try{
PDFWriter writer =
Pdfwriter.getinstance (document,
New FileOutputStream (filename));
Document.open ();
Document.add (New Paragraph ("Hello World"));
Document.close ();
}
catch (Exception e) {}
%>

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.*,
Java.awt.color,com.lowagie.text.*,
Com.lowagie.text.pdf.* "%>
<%
Response.setcontenttype
("Application/pdf");
Document document = new document ();
Bytearrayoutputstream Buffer
= new Bytearrayoutputstream ();
PDFWriter writer=
Pdfwriter.getinstance (document, buffer);
Document.open ();
Document.add (New Paragraph ("Hello World"));
Document.close ();
DataOutput output =
New DataOutputStream
(Response.getoutputstream ());
byte[] bytes = Buffer.tobytearray ();
Response.setcontentlength (bytes.length);
for (int i = 0;
i < bytes.length;
i++)
{
Output.writebyte (Bytes[i]);
}
%>

II generation via servlet

Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import com.lowagie.text.*;
Import com.lowagie.text.pdf.*;
public void Doget
(HttpServletRequest request,
HttpServletResponse response)
Throws Ioexception,servletexception
{
Document document =
New Document (PAGESIZE.A4, 36,36,36,36);
Bytearrayoutputstream BA
= new Bytearrayoutputstream ();
Try
{
PDFWriter writer =
Pdfwriter.getinstance (document, BA);
Document.open ();
Document.add (New
Paragraph ("Hello World"));
}
catch (Documentexception de)
{
De.printstacktrace ();
System.err.println
("A Document Error:" +de.getmessage ());
}
Document.close ();
Response.setcontenttype
("Application/pdf");
Response.setcontentlength (Ba.size ());
Servletoutputstream out
= Response.getoutputstream ();
Ba.writeto (out);
Out.flush ();
}

4. End

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.

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.