当前位置: 技术问答>java相关
Servlet下使用IText作Pdf的打印,为什么IE总是显示一个空白页面?急!急!急!
来源: 互联网 发布时间:2017-04-13
本文导语: Servlet中使用IText作打印,它返回给客户端的却总是一个空白的页面。不知为什么? Servlet的代码如下: package itext; import java.io.PrintWriter; import java.io.IOException; import java.io.File; import java.io.*; import java.util.Date; impor...
Servlet中使用IText作打印,它返回给客户端的却总是一个空白的页面。不知为什么?
Servlet的代码如下:
package itext;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.io.*;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;
public class reportservlettest extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Document document = new Document();
ServletOutputStream out = response.getOutputStream();
try {
//设置ContentType
response.setContentType("application/pdf");
ByteArrayOutputStream ba = new ByteArrayOutputStream();
PdfWriter.getInstance(document, ba);
//设置contentLength
response.setContentLength(ba.size());
ba.writeTo(out);
document.open();
document.add(new Paragraph("Hello World"));
}
catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
document.close();
out.flush();
}
}
Servlet的代码如下:
package itext;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.io.*;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;
public class reportservlettest extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Document document = new Document();
ServletOutputStream out = response.getOutputStream();
try {
//设置ContentType
response.setContentType("application/pdf");
ByteArrayOutputStream ba = new ByteArrayOutputStream();
PdfWriter.getInstance(document, ba);
//设置contentLength
response.setContentLength(ba.size());
ba.writeTo(out);
document.open();
document.add(new Paragraph("Hello World"));
}
catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
document.close();
out.flush();
}
}
|
是不是执行 ba.writeTo(out);太早了
放到document.close();后面试试
放到document.close();后面试试
|
把哪个servlet对应的url加一个.pdf后缀
如
reportservlettest可以在web.xml中映射成report.pdf
如
reportservlettest可以在web.xml中映射成report.pdf
|
try {
response.setContentType("application/pdf");
ServletOutputStream out = response.getOutputStream();
PdfWriter.getInstance(document,out);
document.open();
document.add(new Paragraph("Hello World"));
document.close();
out.flush();
}
catch (Exception e) {
e.printStackTrace(System.err);
}
response.setContentType("application/pdf");
ServletOutputStream out = response.getOutputStream();
PdfWriter.getInstance(document,out);
document.open();
document.add(new Paragraph("Hello World"));
document.close();
out.flush();
}
catch (Exception e) {
e.printStackTrace(System.err);
}