一、直接导出成PDF
import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class ItextProduce { public static void main(String[] args) { Document doc = null; try { doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("C:\\itext.pdf")); doc.open(); doc.add(new Paragraph("Hello World")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } finally { doc.close(); } } }
二、由Servlet导出PDF
package com.lwf.eus.servlet; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class ListRocarsReportServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ListRocarsReportServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Document doc = new Document(); ByteArrayOutputStream ba = new ByteArrayOutputStream(); try { PdfWriter writer = PdfWriter.getInstance(doc, ba); doc.open(); doc.add(new Paragraph("Hello World")); } catch (DocumentException e) { e.printStackTrace(); } doc.close(); response.setContentType("application/pdf"); response.setContentLength(ba.size()); ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); out.flush(); } }
三、由JSP导出成PDF
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.ByteArrayOutputStream, com.itextpdf.text.Document, java.io.DataOutput,java.io.DataOutputStream, com.itextpdf.text.Paragraph, com.itextpdf.text.DocumentException, com.itextpdf.text.Document.*, com.itextpdf.text.pdf.*"%> <% response.setContentType("application/pdf"); Document doc = new Document(); ByteArrayOutputStream ba = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(doc, ba); doc.open(); doc.add(new Paragraph("Hello World")); doc.close(); DataOutput output = new DataOutputStream(response.getOutputStream()); byte[] bytes = ba.toByteArray(); response.setContentLength(bytes.length); for (int i = 0; i < bytes.length; i++) { output.writeByte(bytes[i]); } %>
jsp导出时会报错: org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
报错原因:
JSP调用response.getWriter与response.getOutputStream()相冲突! 解决办法:
在使用完输出流后调用以下两行代码即可: out.clear(); out = pageContext.pushBody();
代码修改为:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.ByteArrayOutputStream, com.itextpdf.text.Document, java.io.DataOutput,java.io.DataOutputStream, com.itextpdf.text.Paragraph, com.itextpdf.text.DocumentException, com.itextpdf.text.Document.*, com.itextpdf.text.pdf.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% response.setContentType("application/pdf"); Document doc = new Document(); ByteArrayOutputStream ba = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(doc, ba); doc.open(); doc.add(new Paragraph("Hello World")); doc.close(); DataOutputStream output = new DataOutputStream(response.getOutputStream()); byte[] bytes = ba.toByteArray(); response.setContentLength(bytes.length); for (int i = 0; i < bytes.length; i++) { output.writeByte(bytes[i]); } output.flush(); //out --jsp out.clear(); out = pageContext.pushBody(); %> </body> </html>