【问题标题】:iText PDF in ServletServlet 中的 iText PDF
【发布时间】:2013-04-05 09:19:40
【问题描述】:

所以,我正在使用此代码从我的服务器创建一个报告 PDF 文件

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。 到目前为止,一切都很好。它有效!

当我调用另一个报告时,问题就开始了……servlet 没有响应。它说:

文档已关闭。您不能添加任何元素。

当然,它在第一次调用时就关闭了。但这是对不同报告的新呼吁。

我猜它与 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); 
    }

【问题讨论】:

  • 显示代码:pdfw = PdfWriter.getInstance( Documento, baos );
  • 私有静态文档 Documento = new Document();
  • 我的意思是,它会返回PdfWriter 的新实例吗?
  • 我真的不知道...正在尝试这样做,但没有找到任何信息..

标签: java pdf servlets itext


【解决方案1】:

这很简单:你创建一个静态文档:

 private static Document Documento = new Document();

然后你调用 close 就可以了:

Documento.close();

所以这个错误是合乎逻辑的。将文档创建为方法属性并将其传递,而不是将其用作静态属性。在 servlet 中使用静态字段只对缓存有用,其他任何事情都是自找麻烦。

【讨论】:

  • 我应该死在地狱里烧死
  • @MarioCaresCabezas 如果每个程序员都这样做,地球上没有程序员了:) lolz
  • @MarioCaresCabezas 事情发生了,不用担心
猜你喜欢
  • 2013-07-07
  • 2012-04-01
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
相关资源
最近更新 更多