Servlet中的iText PDF [英] iText PDF in Servlet
问题描述
所以,我正在使用此代码从我的服务器创建报告PDF文件
So, i'm creating a report PDF file from my server using this code
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
List<Integer> cartas1 = new ArrayList<Integer>();
DeudorDAO DDAO = new DeudorDAO();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos = DocumentoCartaCobranza.CrearDocumento(
getServletContext().getRealPath("static/images/pdf_banner.jpg"),
getServletContext().getRealPath("static/images/firmaJG.png"),
getServletContext().getRealPath("static/images/firmaAB.jpg"),
DDAO.getDatosFullDeudores(cartas1)
);
} catch (DocumentException e) {
e.printStackTrace();
}
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
和
public static ByteArrayOutputStream CrearDocumento(
String imgCabecera,
String imgFirma,
String imgAbogado,
java.util.List<Deudor> carta1) throws DocumentException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
PdfWriter pdfw = null;
pdfw = PdfWriter.getInstance( Documento, baos );
Documento.open();
for (Deudor D : carta1){
//Imagen cabecera
Image imgHead = Image.getInstance(imgCabecera);
//imgHead.setAbsolutePosition(35, 770);
imgHead.scaleAbsolute(125, 40);
Documento.add(imgHead);
Carta1(D);
//Imagen Firma
Image imgSign = Image.getInstance(imgFirma);
//imgHead.setAbsolutePosition(35, 770);
imgSign.scaleAbsolute(110, 105);
Documento.add(imgSign);
Documento.newPage();
}
Documento.close();
}
catch(DocumentException e) {
System.out.println(e.getMessage());
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return baos;
}
所以我的servlet调用一个类,它返回一个ByteArrayOutputStream。
到目前为止一切顺利。它有效!
So my servlet call a class and it return a ByteArrayOutputStream. So far so good. It works !
当我调用另一个报告时,问题就开始了...... servlet没有响应。它说:
The problems begins when i call another report ... The servlet doesn't response. It says:
该文件已经关闭。你不能添加任何元素。
并且,在第一次通话时它被关闭了。但这是对不同报告的新要求。
And, of cours it was closed, by the first call. But this is a new call for a diferent report.
我想它有关于PDFWriter的一些内容......
I guess it had something about the PDFWriter...
谢谢!!
编辑!
万一你问:
private static float Espaciado = 15;
private static Document Documento = new Document();
private static void Carta1(Deudor D) throws DocumentException {
//Cabecera Cuerpo
Paragraph persona = new Paragraph(); persona.add(Chunk.NEWLINE); persona.add(new Chunk("Señor(a)"));
persona.add(Chunk.NEWLINE); persona.add(new Chunk(D.getPaciente().getNombre()).append(" ").
append(D.getPaciente().getApepat()).append(" ").append(D.getPaciente().getApemat()).toString());
persona.add(Chunk.NEWLINE); persona.add(new Chunk(D.getPaciente().getRut().toString()).append("-").append(D.getPaciente().getDV()).toString());
persona.add(Chunk.NEWLINE); persona.add(new Chunk(D.getPaciente().getDireccion()+", "+D.getPaciente().getCiudad()+", "+D.getPaciente().getComuna()));
persona.setAlignment(Element.ALIGN_LEFT);
Paragraph folio = new Paragraph();
Chunk c = new Chunk(D.getIngreso().toString()+"-"+D.getDV(), new Font(folio.getFont().getFamily(), 20, Font.BOLD)); c.setUnderline(0.5f, -1.5f); folio.add(c);
folio.add(Chunk.NEWLINE); folio.add(new Chunk("Ref: Valorización PAM"));
folio.setAlignment(Element.ALIGN_RIGHT);
Paragraph cc = new Paragraph(new Chunk("Estimado Paciente:"));
cc.setAlignment(Element.ALIGN_LEFT); cc.setSpacingAfter(Espaciado);
//Cuerpo
Paragraph p2 = new Paragraph(new Chunk("En CLINICA IQUIQUE S.A. bla bla").toString());
p2.setFirstLineIndent(50); p2.setSpacingAfter(Espaciado); p2.setAlignment(Element.ALIGN_JUSTIFIED);
Documento.add(persona); Documento.add(folio); Documento.add(cc);
Documento.add(p2);
}
推荐答案
这很简单:你创建一个静态文档:
it is quite simple: you create a static document:
private static Document Documento = new Document();
然后你打电话给它关闭:
and then you call close on it:
Documento.close();
因此错误是合乎逻辑的。将文档创建为方法属性并将其传递,而不是将其用作静态。在servlet中使用静态字段只对缓存有用,其他任何东西都要求麻烦。
So the error is logical. Create the document as a method property and pass it on instead of using it as static. Using static fields in servlets is only good for caches, anything else is asking for trouble.
这篇关于Servlet中的iText PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!