Tutorials to .com

Tutorials to .com » Jsp » Xml » Dynamic WEB application in the creation of PDF documents

Dynamic WEB application in the creation of PDF documents

Print View , by: iSee ,Total views: 26 ,Word Count: 1754 ,Date: Tue, 21 Apr 2009 Time: 12:20 AM

In a recent project in logistics, customer requires us to allow users to build a legacy system from a query shipping information Web site, there are three main requirements:
1. Shipping information must be returned to the format of PDF documents;
2.PDF documents must be downloaded through the browser;
3.PDF file must use Adobe Acrobat Reader to read;
Although our team a lot of j2ee Web application development experience, but in the handling of PDF documents do not have much experience of it. We need to find a server-side Web application in the complex where the PDF document of pure java class library. Finally, we found that iText (http://www.lowagie.com/iText/) to fully meet our needs.

IText is a library 1.iText and processing of PDF documents to create open-source pure java class library. Bruno Lowagie and Paulo Soares led the project. IText API enables Java developers to programmatically create PDF documents. iText provides a lot of features:

PDF and FDF files to support a variety of horizontal and vertical size page layout page page hyphenation margin forms the first page footer files encrypted bar code font color JPEG, GIF, PNG and WMF image in an orderly and disorderly shadow watermark list iText document template is an open source database. At this time, iText can be used under two licensing agreements: Mozilla Public License and the LGPL. If you want more information, please refer to iText site. In this article, you will see the application of the iText API. We will set out how the use of server-side applications iText and servlet dynamically generated PDF documents.

2. Started (Getting Started)

First of all, you need a iText Jar file. IText visit site and download the latest version. At this moment, the latest version is 0.99. iText site provides the API documentation and a comprehensive guide.
In addition to iText, we have to use servlet. If you are not familiar with the servlet, you can book through Jason Hunter's "Java Servlet Programming" to learn it. You need a J2EE application server or can run independently of the servlet engine. Open source Tomcat, Jetty and Jboss is a good choice. Below assume that you are using the Jakarta Tomcat 4.1.

1.iText API

easy-to-use iText API. Through the use of iText, you can create a custom PDF documents. iText library from a number of package components below:
In order to generate PDF documents, you need only two packets com.lowagie.text and com.lowagie.text.pdf.
We used these examples of iText categories:
The key is the Document and the PdfWriter. In the creation of PDF documents, you will often use these two categories. Document is a PDF document based on the description of the object. You can call methods provided by Document type to add content to the document. Java.io.OutputStream object through PdfWriter object associated with the Document together.

3. In Web applications iText

At the design stage, you must decide how to use iText. We use the bottom of the technological development of our Web applications.

1.A technology in the server file system to create PDF documents. Applications java.io.FileOutputStream wrote the document on the server file system. Users through the HTTP GET method to download the file.

2.B technology used in memory java.io.ByteArrayOutputStream create PDF documents. Application through the servlet output stream of bytes of the PDF document sent to the client.
As a result of the application documents do not need to write file system so that services can guarantee the environment in the cluster can work properly, so I prefer to use B technology. If your application environment running on clusters and server clusters do not provide the function of session affinity, A technology may lead to failure.

3. Examples: PDFServlet

Examples of our application by a class composed of: PDFServlet. B used the servlet technology. OutputStream output stream is java.io.ByteArryOutputStream. Using ByteArrayOutputStream, PDF document bytes will be stored in memory. When PDFServlet receive an HTTP request, it will dynamically generate a PDF document and the document sent to the client.
Class javax.servlet.http.HttpServlet extends PDFServlet category and iText into two packages: com.lowagie.text and com.lowagie.text.pdf.
doGet method covering the majority of servlet and the doPost method doGet method. We are no different from the servlet. PDFServlet category covers the doGet method. The servlet will receive the HTTP GET request generates a PDF document.
Part of the core, servlet's doGet method to do the following work:
1. To create a PDF document that contains the object ByteArrayOutputStream bytes;
2. Reponse targets set in the HTTP response to the first content;
3. Get servlet output stream;
4. The document servlet writes the output byte stream;
5. Refresh servlet output stream;
generatePDFDocumentBytes method generatePDFDocumentBytes method responsible for creating PDF documents. In this method the three most important is the Document Object object, ByteArrayOutputStream objects and object PdfWriter. ByteArrayOutputStream associated PdfWriter use Document.
Document doc = new Document ();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream ();
PdfWriter docWriter = null;
docWriter = PdfWriter.getInstance (doc, baosPDF);
/ / ...
With ways to add content to add in the Document.
doc.add (new Paragraph (
"This document was created by a class named:"
+ This.getClass (). GetName ()));

doc.add (new Paragraph (
"This document was created on"
+ New java.util.Date ()));

When you add the content, it is necessary to close the Document object and PdfWriter.
doc.close ();
docWriter.close ();
When, after the closure of the document, ByteArrayOutputStream object to return to the caller.
return baosPDF;
ByteArrayOutputStream contains all the byte PDF documents.
HTTP response header in this application, we are only concerned about the four HTTP response header: Content-type, Content-disposition, Content-length, and Cache-control. If you are not used from the HTTP header, please refer to HTTP 1.1 specification.
Research in the doGet method of PDFServlet, you will be aware of the need to write any data in the servlet output stream before the HTTP response to the first content set, this is very important and subtle point.
Let us explain in more detail the meaning of each response to the first.

In the servlet in, HttpServletResponse response there is a show that contains the parameters of the type of content. On the PDF document, the content type is application / pdf. If the servlet does not set any type, web browser, it is difficult to decide how to deal with this document.
Use the code below PDFServlet set content type:
resp.setContentType ( "application / pdf");
Content-disposition header to the browser to determine the content of HTTP response messages. When the first browser to read these information, it can determine:

HTTP response contains a document;

Included in the response file name;

The document is displayed in the browser to use the main window or the application of an external view;
RFC 2183 in the Content-disposition header for a complete explanation.
Through the appropriate set of Content-disposition value, servlet instructions to the browser is "embedded" or to show the document as attachment.
Example 1. Embedded display a document Content-disposition: inline; filename = foobar.pdf
Example 2. In response to a file attached Content-disposition: attachment; filename = foobar.pdf
Pseudo-code below shows how to set up the first message:
public void doGet (HttpServletRequest req, HttpServletResponse resp)
/ / ...
resp.setHeader (
"inline; filename = foobar.pdf");
/ / ...
According to the different characteristics of your application, you can let the browser cache or not cache your PDF documents are generated. Server-side application can have a variety of head control HTTP content cache. Below are some examples:

Cache-Control: no-cache

Cache-Control: no-store

Cache-Control: must-revalidate

Cache-Control: max-age = 30

Pragma: no-cache

Expires: 0
Cache-Control header on the full explanation, see HTTP 1.1 specification.
PDFServlet the Cache-Control set to max-age = 30. The header information tells the browser to cache the document, the longest for 30 seconds.
Content-length header must be set to PDF document byte values. If Content-length not set correctly, your browser may not be able to correctly display the document. Is the example code below:
ByteArrayOutputStream baos = getByteArrayOutputStream ();
resp.setContentLength (baos.size ());
The PDF document to the Web browser PDFServlet byte stream by the servlet output stream writes the way to PDF documents to the client. It by calling the getOutputStream method of the HttpServletResponse object to get the output stream. getOutputStream method returns an object of type javax.servlet.ServletOutputStream.
ServletOutputStream sos;
sos = resp.getOutputStream ();

Java and XML Articles

Can't Find What You're Looking For?

Rating: 5.0


No comments posted.