{oapdf_gg}
3. examples: PDFServletExamples 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 work of the following: 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 ()));< br/> doc.add (new Paragraph ( "This document was created on" + New java.util.Date ()));< br/> 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. Content-type 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 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: 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 file Content-disposition: inline; filename = foobar.pdf Example 2. Surcharges in response to a file Content-disposition: attachment; filename = foobar.pdf Pseudo-code below shows how to set up the first information: public void doGet (HttpServletRequest req, HttpServletResponse resp) ( //... resp.setHeader ( "Content-disposition", "inline; filename = foobar.pdf"); //... ) Cache-Control 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 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 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 ());< br/> the PDF document to the Web browser PDFServlet byte stream by the servlet output stream writes the way the PDF document 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 (); baos.writeTo (sos); sos.flush (); All the data in the stream after the write, call flush () method of all the bytes sent to the client. packaging and deployment In order to run Tomcat in PDFServlet, you need to be packaged in the application WAR file. iText JAR file (itext-0.99.jar) must be placed on the WAR file lib directory below. If you forgot to pack into the iText JAR file, servlet will be reported to a java.lang.NoClassDefFoundError error and stop running. run the application Deployed in the WAR file, you are ready to test a servlet. Jakarta Tomcat listening on port 8080 requests. Request in a browser http://hostname:8080/pdfservlet/createpdf . servlet will execute and return a PDF document browser. 4.iText outside the programiText provides a number of PDF documents generated underlying API. However, it is not effective for any application. In my daily work, I combined with Microsoft Word and Adobe Acrobat to use iText. First, we use Microsoft Word's team designed a ship form. We use Acrobat to convert Word documents into PDF documents. Then, we use iText template function, we have PDF files into our applications. From here, fill in the data table and output the final PDF document is very easy. Statements based on Web applications, such as a tool for JasperReports, which provides a higher level than the abstract iText. 5. summing upWhen your application needs to dynamically create a PDF document when, iText class library is a good program. You can enhance and expand the code in this article to experience the ability to iText. Soon, due to the provision of a comprehensive PDF document, you will give your colleagues and clients impressed. |