在 Struts 2 中动态创建 PDF [英] Dynamically Creating PDF in Struts 2

查看:29
本文介绍了在 Struts 2 中动态创建 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好;

我在处理 struts2 Web 应用程序时遇到问题.我正在使用数据库动态创建 PDF.我想在网页中显示它,但我不知道我是怎么做的,有人可以帮助我.

I have a problem that I am working on struts2 web application. I am dynamically creating a PDF using data base. i want to show it in a web page but I don`t know how I do it is any one can help me.

谢谢...

推荐答案

操作代码:

public class PDFAction extends ActionSupport {
    private InputStream inputStream;

public String getPDF(){       
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        PdfWriter.getInstance(document, buffer);

        document.open();
        Paragraph p = new Paragraph();
        p.add("INSTITUTO POLITÉCNICO NACIONAL, ESCUELA SUPERIOR DE CÓMPUTO, DIEGO A. RAMOS");

        document.add(p);
        document.close();

        inputStream  =  new ByteArrayInputStream(buffer.toByteArray());

   return SUCCESS;
}

public InputStream getInputStream() {
    return inputStream;
}

public void setInputStream(InputStream inputStream) {
    this.inputStream = inputStream;
}
}

Struts.xml:

Struts.xml:

<action name="getPDF" class="action.PDFAction" method="getPDF">
       <result name="success" type="stream">
            <param name="inputName">inputStream</param> 
            <param name="contentType">application/pdf</param> 
            <param name="contentDisposition">filename="mypdf.pdf"</param> 
            <param name="bufferSize">2048</param>
        </result>
</action>

试试吧,它就像一种魅力,非常适合我.如果您有疑问,请阅读有关 Struts 2 提供的流结果类型的更多信息.这个问题的答案很简单,但很难做到.

Try it, it works like a charm, works perfect for me. If you are in doubt read more about stream result type that Struts 2 provides. The answer to this is so simple yet it was hard to get to it.

这篇关于在 Struts 2 中动态创建 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆