JSP AND PDF : iText - Generate PDF from Servlet or JSP with iText
หน้าแรก JSP JSP AND PDF : iText - Generate PDF from Servlet or JSP with iText
This tutorial assume you had some experience with Servlet and JSP technology, know how to setup and run web container such as Apache Tomcat and compile/install Servlet.
Copy and paste itext-1.4.8.jar(or latest itext jar) into your web application WEB-INFLIB folder. Please refer toSetup iText for project & Hello Worldtutorial to find out where to obtain itext-1.4.8.jar file. This allow servlet and jsp in your web application gain access to iText library.
Generate PDF from Servlet with iText
Let's take a look at following example of a Servlet that generate PDF file with iText.
package com.geek.tutorial.itext.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import java.io.*;
import java.util.*;
public class PDFServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("application/pdf"); // Code 1
Document document = new Document();
try{
PdfWriter.getInstance(document,
response.getOutputStream()); // Code 2
document.open();
// Code 3
PdfPTable table = new PdfPTable(2);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
// Code 4
document.add(table);
document.close();
}catch(DocumentException e){
e.printStackTrace();
}
}
}
Code 1.
Set Content type of the servlet to PDF. This will invoke PDF viewer on client machine to handle the PDF content.
Code 2.
Pass ServletOutputStream into PDFWriter for generate PDF.
Code 3.
Construct a PDF table for demonstration of this tutorial.
Code 4.
Add the table into the document and close it.
Compile the example above and place the result PDFServlet.class into your web application WEB-INFclassescomgeektutorialitextservlet folder. Note that you need servlet-api.jar added into your compiler classpath or your will get a class not found exception. One way you can find that servlet-api.jar is in installed tomcat commonlib directory.
Next add the following XML snippet into your web application's web.xml file.
<servlet>
<servlet-name>PDFServlet</servlet-name>
<servlet-class>com.geek.tutorial.itext.servlet.PDFServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PDFServlet</servlet-name>
<url-pattern>/PDFServlet</url-pattern>
</servlet-mapping>
Now you can test your servlet with internet browser. For example, if the root URL of your web application is http://localhost:8080/test/, then you should enter URL as http://localhost:8080/test/PDFServlet. The servlet will generate following PDF inside internet browser.
Display a Save As dialog box instead of view in the browser
If you want to force the user to save the PDF into their local drive instead of view the generate PDF in browser, you can add the following code snippet right after setting the content type of servlet
...
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
" attachment; filename="example.pdf"");
...
Generate PDF from JSP with iText
In case you do not has the access-right to deploy servlet into target web-server, the alternative way is generate PDF from JSP file. Code below shows a JSP that generate PDF content.
<%@
page import="java.servlet.*,
javax.servlet.http.*,
java.io.*,
java.util.*,
com.lowagie.text.pdf.*,
com.lowagie.text.*"
%><%
response.setContentType("application/pdf");
Document document = new Document();
try{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
document.open();
PdfPTable table = new PdfPTable(2);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
document.add(table);
document.close();
DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for(int i = 0; i < bytes.length; i++)
{
dataOutput.writeByte(bytes[i]);
}
}catch(DocumentException e){
e.printStackTrace();
}
%>
Take extra care on anything outside of <% %>, do not leave any newline or space outside of <% %> or you will corrupt the generated PDF file. Save this JSP file into your web application and execute it, you will get the same output as its servlet version.
Your donation will be use for this project's site maintainance and further development of the content. Your support can help us provide higher quality of free tutorials and services to everyone in future
. Download latest source code for this project.
refer: http://www.geek-tutorials.com/java/itext/servlet_jsp_output_pdf.php
ขึ้นไปด้านบน
