Bgarbarek
Bgarbarek

Reputation: 39

Generate a PDF document and open it in a new window with java bean

I'm trying to generate a PDF document and open it in a new window. I create a Servlet to create the PDF and in my backing bean I have this code:

public void viewReport(){
        try {
            FacesContext.getCurrentInstance()
               .getExternalContext().redirect("/app/report.pdf?type=sb");
            return;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

and in my page I have this:

<h:form target="_blank">
<h:commandButton action="#{clientBean.viewReport}" value="#{msgs['button.view']}"/>
</h:form>

The PDF is ok but it opens in the same window. How can I open the PDF in a new window from bean?

Upvotes: 2

Views: 8041

Answers (2)

mjn
mjn

Reputation: 36644

See this example

<h:form target="_blank">
    <h:commandButton value="Download PDF" action="#{myBean.downloadPDF}" />
</h:form>

It uses the same code as your example, the difference is in the backing bean code which does not perform a redirect but instead directly creates and returns a PDF document in the response stream.

Upvotes: 0

Elias Dorneles
Elias Dorneles

Reputation: 23806

What about you do it with plain HTML, instead of doing hacks with the bean the server-side?

<a href="/app/report.pdf?type=sb" target="_blank" />#{msgs['button.view']}</a>

Upvotes: 1

Related Questions