user1471970
user1471970

Reputation: 31

Struts2 Display pdf file in jsp

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.

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

Upvotes: 3

Views: 12508

Answers (3)

rvazquezglez
rvazquezglez

Reputation: 2454

I'm doing it using an action with inputStream as Anu suggested. And using the PDF.JS library.

Online demo

GitHub

Upvotes: 0

Michael Santos
Michael Santos

Reputation: 11

public ByteArrayInputStream generatePDF(List<Object> items) {

    try {

        List<InputStream> listInputStream = new ArrayList<InputStream>();
        for (int i = 0; i < items.size(); i++) {
            listInputStream.add(new ByteArrayInputStream(getBytes(items.get(i)));
        }

        HttpServletResponse response = ServletActionContext.getResponse();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, buffer);
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        for (InputStream inputStream : listInputStream) {

            PdfReader reader = new PdfReader(inputStream);

            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                document.setPageSize(reader.getPageSize(i));
                document.newPage();
                PdfImportedPage page = writer.getImportedPage(reader, i);
                cb.addTemplate(page, 0, 0);
            }
        }

        document.close();

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

        return new ByteArrayInputStream(bytes);

    } catch(Exception e){
        System.out.println(e);
    }

}

Upvotes: 0

Anupam
Anupam

Reputation: 8016

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;
    }
}

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>   

Upvotes: 8

Related Questions