How to generate PDF on the browser using JSF(Java) and Apache FOP

1.5k Views Asked by At

I am a newbie in the JSF and Apache FOP environment. I am using JSF (2.2.7) Primeface 5.0 (Eclipse IDE_Luna) and created a method named CreatePDF to generate PDF via JSF on the browser. I am using Apache FOP (1.1) which uses XML data and corresponding XSLT(XSL-FO) to generate the PDF. I have placed my xml and xslt file under the Web-Content JSF Project folder.

My problem is, when I press the command button in the browser, The chrome/Firefox browser displaying the PDF window and shows the message “Failed to Load PDF Document”. First I thought, its may be Adobe or my xml and xsl-fo writing problem but when I used thses xml and xslt file in the JSP project - the browser window can easily able to generate the PDF on the fly. In the JSP page i used the FOP uriResolver API to get the data from the Application project folder but in JSF i don't know how to use this API.

So may be JSF transformation with FOP Factory and file declaration format has some problem.

The JSF Java Clas is below:

    public void CreatePDf(){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse)     externalContext.getResponse();
        String basePath = externalContext.getRealPath("/");
        try{
            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream(), 10240);
            File xsltfile = new File(basePath+"xsl/peopleconvertor.xsl");
            File xmlfile = new File(basePath+ "xml/people.xml");
            FopFactory fopFactory = FopFactory.newInstance();
            fopFactory.setBaseURL(basePath);
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            foUserAgent.setBaseURL(fopFactory.getBaseURL());
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsltfile));
            Source src = new StreamSource(xmlfile);
            Result res = new SAXResult(fop.getDefaultHandler());
            transformer.transform(src, res);
            response.setContentType("application/pdf");
    } catch (Exception e) {
        e.printStackTrace(System.err);}
    facesContext.responseComplete();
}}

And my XHTML (Primeface) command button for JSF:

 p:commandButton id="tabelle1" action="#{Jsf_PDF.CreatePDf()}"
            immediate="true" icon="ui-icon-check" value="Tabelle erzeugen"
        style="position:relative; margin-top: 20px; right:-950px" ajax="false"

The JSP Java Class:

<pre>CreatePDf(){
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse)   context.getExternalContext().getResponse();
        try{
        ByteArrayOutputStream out = new ByteArrayOutputStream();

            fopFactory.setURIResolver(uriResolver);
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
            Source xslSrc = this.uriResolver.resolve("servlet-context:/xsl/peopleconvertor.xsl", null);
            tFactory.setURIResolver(uriResolver);
            Transformer transformer = tFactory.newTransformer(xslSrc);  
            transformer.setURIResolver(uriResolver);
            Result res = new SAXResult(fop.getDefaultHandler());
            Source xmlSrc = uriResolver.resolve("servlet-context:/xml/people.xml", null);
            transformer.transform(xmlSrc, res);
            response.setContentType("application/pdf");
            response.setContentLength(out.size());
            response.getOutputStream().write(out.toByteArray());
            response.getOutputStream().flush();
        }catch (Exception e) {
              e.printStackTrace(System.err);   
        }
          context.responseComplete();   
    }

Please help me how do i solve this problem with JSF......

0

There are 0 best solutions below