sahi hai
sahi hai

Reputation: 99

JSF2.0 open a pdf file of XHTML view using command button

I tried to export current jsf page to pdf. Code as follows:

Page with command button index.html

<?xml version='1.0' encoding='UTF-8' ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html">
   <h:head>
    <title>Facelet Title</title>
  </h:head>
  <h:body>
    <h:form>
        <h:inputText value="#{bean.name}" id="name" ></h:inputText>
        <h:commandButton value="click" id="btn" actionListener="#{bean.writePDF}"></h:commandButton>
    </h:form>
</h:body></html>

writePDF() function for action button is as follows:

public void writePDF() {
     String PDF_FILE_NAME = "converted.pdf";
     FacesContext facesContext = FacesContext.getCurrentInstance();
     ExternalContext externalContext =facesContext.getExternalContext();
     HttpServletRequest request = (HttpServletRequest)externalContext.getRequest();
     HttpSession session = (HttpSession) externalContext.getSession(true);
     String url = request.getRequestURL().append(";jsessionid=").append(session.getId().toString()).toString();
     try
     {
         ITextRenderer renderer = new ITextRenderer();
         renderer.setDocument(new URL(url).toString());
         renderer.layout();
         HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();
         response.reset();
         response.setContentType("application/pdf");
         response.setHeader("Content-Disposition", "inline;filename=\"" + PDF_FILE_NAME + "\"");
         ServletOutputStream browserStream = response.getOutputStream();
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         renderer.createPDF(browserStream);
         renderer.finishPDF();
         facesContext.responseComplete();
     }
     catch (Exception ex)
     {
         ex.printStackTrace();
                  }
     facesContext.responseComplete();
 }

On click of command button only a blank pdf page is generating with no control and value in it. Am I doing something wrong, kindly suggest.

Thanks in advance.

Upvotes: 0

Views: 4187

Answers (1)

BalusC
BalusC

Reputation: 1108692

I just did a test with exactly the given code. It indeed doesn't show anything. It turns out that Flyingsaucer simply doesn't eat HTML form elements, as already guessed in my first comment on your question.

Your best bet is really to present data by output texts instead of input texts. To achieve this dynamically, you might want to add a request parameter which triggers the conditional rendering of inputs/outputs.

E.g.

<h:inputText value="#{bean.name}" id="name" rendered="#{not param.pdf}"/>
<h:outputText value="#{bean.name}" rendered="#{param.pdf}"/>

and then add a pdf=true to the query string.

String url = request.getRequestURL().append(";jsessionid=").append(session.getId()).append("?pdf=true").toString();

Or just create a different page which uses the same bean and then pass its URL instead.

Upvotes: 1

Related Questions