Dynamically create PDF files in Web Applications

Source: Internet
Author: User
Tags adobe acrobat reader
ArticleDirectory
    • Dynamically create PDF files in Web Applications


Dynamically create PDF files in Web Applications
Gagaghost translation (Participation score: 104, expert score: 230) published: PM version: 1.0 read:885Times

Dynamically create PDF files in Web Applications

Original article: Sean C. Sullivan Translation: gagaghost
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.
    The slave servlet uses the followingCodeSet 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, the servlet will report a java. Lang. noclassdeffounderror error and stop running.
    Run the application
    After deploying the war file, you have prepared the test servlet. Jakarta Tomcat listens for requests on port 8080.
    Request http: // hostname: 8080/servlet/createpdf in the browser. Servlet will execute and return a PDF file in the browser.
    4. solutions other than itext

    Itext provides many underlying APIs for generating PDF documents. However, it is not effective for any application.
    In my daily work, I use itext in combination with Microsoft Word and Adobe Acrobat. First, our team designed a shipping form using Microsoft Word. Then, we use Acrobat to convert a Word document into a PDF document. Then, we use the itext template function to load the PDF file into our application. From here, it is quite easy to enter the data in the form and output the final PDF document.
    For reports-based Web applications such as jasperreports, it provides high-level abstraction than itext.
    5. Summary

    When your application needs to dynamically create PDF files, the itext class library is a good solution. You can enhance and expand the code in this article to experience the itext capability. Soon, thanks to the comprehensive PDF documentation, you will be impressed by your colleagues and customers.
    6. Other resources

    Http://www.lowagie.com/iText/
    Www.jpedal.org
    Www.w.box.org
    Xml.apache.org/fop
    HTTP 1.1 protocol specification
    RFC 2183
    Dmoz.org/computers/data_formats/document/publishing/pdf
    Www.planetpdf.com
    Www.mongozone.com

  • 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.