Dynamically create PDF files in Web Applications

Source: Internet
Author: User
Tags adobe acrobat reader

In a recent logistics project, the customer asked us to create a Web site that allows users to query shipment information from a legacy system. There are three main requirements:
1. shipment information must be returned in PDF format;
2. pdf files must be downloaded in a browser;
3. pdf files must be read using Adobe Acrobat Reader;
Although our team has a lot of J2EE Web application development experience, we have little experience in PDF document processing. We need to find a pure Java class library that can generate complex PDF documents in Web applications on the server side. Finally, we found that iText (Http://www.lowagie.com/iText/) Can fully meet our needs.

1. iText class library

IText is an open-source pure Java class library that creates and processes PDF documents. Bruno Lowagie and Paulo Soares lead the project. The IText API allows Java developers to create PDF documents programmatically. IText provides many features:

Support for PDF and FDF documents

Various page sizes

Horizontal and vertical layout

Margins

Table

Broken word

Page header

Footer

Page number

Bar Code

Font

Color

Document Encryption

JPEG, GIF, PNG, and WMF images

Ordered and unordered list

Shadow

Watermark

Document Template
IText is an open source library. When writing this article, iText can use Mozilla Public License and LGPL under two License protocols. For more information, see the iText website. In this article, you will see the iText API application. We will explain how to use iText and servlet to dynamically generate PDF documents in server applications.

2. Start (Getting Started)

First, you need an iText Jar file. Visit the iText site and download the latest version. When writing this article, the latest version is to make 0.99. The iText site provides API documentation and a comprehensive guide.
In addition to iText, we also need to use servlet. If you are not familiar with servlet, you can learn it through Jason Hunter's book Java Servlet Programming. You need a J2EE application server or a servlet engine that can run independently. Open source software Tomcat, Jetty and Jboss are good choices. Assume that you are using Jakarta Tomcat 4.1.

1. iText API

The iText API is easy to use. By using iText, you can create custom PDF documents. The iText library consists of the following packages:
Com. lowagie. servlets
Com. lowagie. text
Com.lowagie.text.html
Com. lowagie. text. markup
Com.lowagie.text.pdf
Com.lowagie.text.html. codec
Com.lowagie.text=. hyphenation
Com.lowagie.text=. wmf
Com. lowagie. text. rtf
Com. lowagie. text. xml
Com. lowagie. tools
To generate the authorization file, you only need two packages: com.lowagie.textand com.lowagie.text.pdf.
These iText classes are used in our example:
Com.lowagie.text.html. writable writer
Com. lowagie. text. Document
Com. lowagie. text. HeaderFooter
Com. lowagie. text. Paragraph
Com. lowagie. text. Phrase
Com. lowagie. text. Table
Com. lowagie. text. Cell
The key classes are Document and plain writer. These two classes are frequently used when creating PDF documents. Document is an object-based description of a PDF Document. You can add content to the Document by calling the methods provided by the Document class. The upload writer object is associated with the Document through the java. io. OutputStream object.

3. Use iText in Web Applications

In the design phase, you must decide how to use iText. We used the following technologies to develop our Web applications.

1. A Technology

Create a PDF file on the server file system. The application uses java. io. FileOutputStream to write the file to the server file system. You can use the http get method to download the object.

2. Skill B

Use java. io. ByteArrayOutputStream to create a PDF file in the memory. The application sends the PDF file byte to the client through the servlet output stream.
Because the application does not need to write files to the file system, this can ensure normal operation in the cluster service environment, so I prefer to use B technology. If your application runs in A cluster and the server cluster does not provide session affinity, A technology may fail.

3. Example: Servlet

Our example application consists of a class: servlet. This servlet adopts the B technique. The output stream OutputStream is java. io. ByteArryOutputStream. ByteArrayOutputStream is used to store PDF file bytes in memory. When servlet receives an HTTP request, it dynamically generates a PDF file and sends it to the client.
The servlet class extends the javax. servlet. http. HttpServlet class and imports two iText packages: com.lowagie.textand com.lowagie.text.pdf.
DoGet Method
Most servlets cover a method in the doPost and doGet methods. Our servlet is no different. The javasservlet class overwrites the doGet method. The servlet generates a PDF file after receiving an http get request.
In the core part, the servlet doGet method does the following:
1. Create a ByteArrayOutputStream object containing the PDF file bytes;
2. Set the HTTP response header content on the reponse object;
3. Get the servlet output stream;
4. Write the document bytes to the servlet output stream;
5. Refresh the servlet output stream;
Generateappsdocumentbytes Method
Generateacrodocumentbytes is used to create PDF documents. The three most important objects in this method are the Document Object, ByteArrayOutputStream object, and javaswriter object. PdfWriter uses ByteArrayOutputStream to associate a Document.
Document doc = new Document ();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream ();
Specified writer docWriter = null;
DocWriter = Specified writer. getInstance (doc, baosPDF );
//...
Add the content to the Document using the add method.
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 ()));

After adding the content, close the Document and author writer objects.
Doc. close ();
DocWriter. close ();
When the document is closed, the ByteArrayOutputStream object is returned to the caller.
Return baosPDF;
ByteArrayOutputStream contains all the bytes of the PDF document.
HTTP Response Header
In this application, we only focus on four HTTP Response Headers: Content-type, Content-disposition, Content-length, and Cache-control. If you have never used an HTTP header, refer to the HTTP 1.1 Standard.
Studying the doGet method in servlet, you will notice that it is important and subtle to set the HTTP Response Header before any data is written to the servlet output stream.
Let's describe the meaning of each response header in more detail.
Content-type


In servlet, HttpServletResponse has a parameter indicating the content type contained in the response. For pdf files, the content type is application/pdf. If the servlet does not set the type, it is difficult for the web browser to decide how to process the file.
Servlet uses the following code to set the content type:
Resp. setContentType ("application/pdf ");
Content-disposition
The Content-disposition Header is provided to the browser to determine the HTTP Response Content. After the browser reads the header information, it can determine:

The HTTP response contains a file;

File name included in the response;

Whether the file is displayed in the main browser window or viewed by external applications;
RFC 2183 provides a complete explanation of the Content-disposition Header.
By appropriately setting the Content-disposition value, the servlet can indicate whether the browser displays the file embedded or treats it as an attachment.
Example 1. Embedded display of a file
Content-disposition: inline; filename?foobar=
Example 2. append a file to response
Content-disposition: attachment; filename?foobar=
The following pseudo code illustrates how to set the header information:
Public void doGet (HttpServletRequest req, HttpServletResponse resp)
{
//...
Resp. setHeader (
"Content-disposition ",
"Inline; filename?foobar= ");
//...
}
Cache-Control
Depending on the features of your application, you can make the browser cache or not cache the PDF file you are generating. Server applications can have multiple HTTP headers to control content caching. 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
For a comprehensive explanation of the Cache-Control header, see the HTTP 1.1 Standard.
Servlet sets Cache-Control to max-age = 30. This header tells the browser that the Maximum Cache Time for this file is 30 seconds.
Content-length
The Content-length header must be set to a byte value in the PDF file. If Content-length is not set correctly, the browser may not display the file correctly. The following is the sample code:
ByteArrayOutputStream baos = getByteArrayOutputStream ();
Resp. setContentLength (baos. size ());
Send PDF documents to Web browsers
Servlet sends the PDF file to the client by writing the byte stream to the servlet output stream. It calls the getOutputStream method of the HttpServletResponse object to obtain the output stream. The getOutputStream method returns an object of the javax. servlet. ServletOutputStream type.
ServletOutputStream sos;
Sos = resp. getOutputStream ();
Baos. writeTo (sos );
Sos. flush ();
After writing all the data to the stream, call the flush () method to send all the bytes to the client.
Packaging and deployment
To run the volume servlet in Tomcat, You need to package the application in the WAR file. The iText JAR file (itext-0.99.jar) must be placed under the lib directory of the WAR file. If you forget to package the iText JAR file

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.