Struts2在jsp中显示pdf文件

use*_*970 3 java struts2 itext

我的要求是使用来自数据库的一些数据创建一个动态报告pdf文件,我正在使用iText.现在,我想在网页中显示这个pdf文件以及菜单,页眉,页脚等.

因此,如果用户有一些pdf查看器,则此pdf应显示在具有打印选项的用户计算机上以打印该pdf.

anu*_*anu 8

这就是我这样做的方式.您可以在iframe或常规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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的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>   
Run Code Online (Sandbox Code Playgroud)