iText PDF in Servlet

itext, java, pdf, servlets

Solution

it is quite simple: you create a static document:

 private static Document Documento = new Document();

and then you call close on it:

Documento.close();

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.

Problem

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(); ``` And ``` 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; } ``` So my servlet call a class and it return a ByteArrayOutputStream. So far so good. It works ! The problems begins when i call another report ... The servlet doesn't response. It says: The document has been closed. You can't add any Elements. And, of cours it was closed, by the first call. But this is a new call for a diferent report. I guess it had something about the PDFWriter... Thanks !! EDIT ! Just in case you ask: ``` 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); } ```

Original source