3

所以,我正在使用此代码从我的服务器创建一个报告 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); 
    }
4

1 回答 1

5

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

 private static Document Documento = new Document();

然后你打电话给它:

Documento.close();

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

于 2013-04-12T18:40:37.227 回答