利用iText在JSP中生成PDF报表 itest.jar 和 iTextAsian.jar包(csdn上有下载的) 1.直接在服务器上生成PDF文件。 在document.open();和document.close();两条语句中间加入自己希望放在PDF文件中的内容即可。该例子只在PDF文件中加了“Hello World“一行文字。
///test.java///
package test;
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class test {
public static void writepdf() throws Exception {
Document document = new Document(PageSize.A4);
try {
PdfWriter.getInstance(document,new FileOutputStream("Chap0101.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
public static void main(String[] args) throws Exception {
writepdf(); // 在工程目录下生成一个pdf
}
}
2将PDF文件通过流的形式输送到客户端的缓存。这样做的好处是不会在服务器上留下任何“遗迹”。 使用servlet
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class OutPdf extends HttpServlet {
/**
* Constructor of the object.
*/
public OutPdf() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Document document = new Document(PageSize.A4, 36, 36, 36, 36);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
try {
PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
document.add(new Paragraph("Hello World!!"));
} catch (DocumentException de) {
de.printStackTrace();
System.err.println("A Document error:" + de.getMessage());
}
document.close();
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
web.xml中
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>OutPdf</servlet-name>
<servlet-class>test.OutPdf</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OutPdf</servlet-name>
<url-pattern>/servlet/OutPdf</url-pattern>
</servlet-mapping>
使用<a href="/servlet/OutPdf">输出pdf</a>
//输出是OutPut.pdf ?怎么给pdf命名呀