Struts2在jsp中显示pdf文件 [英] Struts2 Display pdf file in jsp

查看:490
本文介绍了Struts2在jsp中显示pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是创建一个动态报告pdf文件,其中包含数据库中的一些数据,我正在使用iText进行操作。
现在,我想在网页中显示这个pdf文件以及菜单,页眉,页脚等。

My requirement is to create a dynamic report pdf file with some data from database which I'm doing it using iText. Now, I want to display this pdf file inline in the webpage alongwith menu,header, footer, etc.

所以,如果用户有一些pdf查看器然后这个pdf应该显示在用户机器上,并带有打印选项以打印pdf。

So, If the user has some pdf viewer then this pdf should be displayed in user machine with print option to print that pdf.

推荐答案

这就是我这样做的方式。您可以在iframe或常规jsp中调用此操作

This is how I am doing it. You can call this action inside an iframe or in a regular jsp

public class GeneratePdf extends ActionSupport{
    private InputStream inputStream;
    public String execute(){
        HttpServletResponse response = ServletActionContext.getResponse();
        Document document = new Document();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(document, buffer);
            document.open();
                        // do your thing
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        byte[] bytes = null;
        bytes = buffer.toByteArray();
        response.setContentLength(bytes.length);

        if(bytes!=null){
            inputStream = new ByteArrayInputStream ( bytes );
        }
 return SUCCESS;
}

public InputStream getInputStream() {
        return inputStream;
    }
}

在你的struts.xml中

In your struts.xml

   <action name="GeneratePdf" class="com.xxx.action.GeneratePdf">
    <result name="success" type="stream">
            <param name="contentType">application/pdf</param>
            <param name="inputName">inputStream</param>
            <param name="contentDisposition">filename="test.pdf"</param>
            <param name="bufferSize">1024</param>
    </result>
   </action>   

这篇关于Struts2在jsp中显示pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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