3 Replies Latest reply on Aug 4, 2015 10:24 AM by baddeley84

    Rendering PDF

    rafaelfrade

      I'm generating a pdf using specific api of seam to do that.
      I'd like to know if there is a way to render it without
      showing it to the user.
      For example, when the user clicks at a link, I save the pdf
      in a directory without the user realize it. Like a way to render
      the xhtml that is the pdf(that uses the tags p ), but with no
      need to show it to the user.


      Another question is, using the same API, can I include another
      pdf files(real pdfs, not xhtml) to the one I'm making.


      Thank you.

        • 1. Re: Rendering PDF
          lvdberg

          Hi,


          Although the PDF-support ins Seam is great, nothing beats using the API's directly. It takes some effort, but the result is better.
          In my opinion PDF is a bit to closely coupled, but that's something for another Thread.


          Look at the iText API, or use Birt/JasperReports. The latter has also a nice API to dynamically generate reports. In this way you create the report in the format you want and - if needed - you can output (stream) it directly to your user.


          Leo

          • 2. Re: Rendering PDF
            cathyben

            I found a C#.net PDF rendering API. And it have related pdf reading and edition feature you wanted.

            If you think it's helpful, you can have a look at it.

             

            Best regards.

            • 3. Re: Rendering PDF
              baddeley84

              You should be able to do this in Seam, I have done something similar to email an Excel document as an attachment, see code below, I am sure you could adapt this for Seam PDF

              In the .xhtml file you need to set the export key, this places the generated document in the event scope...eg:

               

              
              <e:workbook xmlns:e="http://jboss.org/schema/seam/excel"
               xmlns:f="http://java.sun.com/jsf/core"
               exportKey="_pue"
               filename="#{poolReportsActions.reportFileName}">
              


              then the following backing bean code to retrieve the report as a byte array:


              //Render the report using a fake request
                EmptyFacesContext emptyFacesContext = new EmptyFacesContext();
              
                try {
                     String pageLocation = "/somePage.xhtml";
                     Renderer renderer = Renderer.instance();
                     renderer.render(pageLocation)
                     //Retrieve the report from the event context
                     if(Contexts.getEventContext().get("_pue")!=null){
              
                          DocumentData data = (DocumentData) Contexts.getEventContext().get("_pue");
                          ByteArrayDocumentData byteData = null;
                          if (data instanceof ByteArrayDocumentData) {
                               byteData = (ByteArrayDocumentData) data;
                          } else {
                               throw new IllegalArgumentException("Couldnt get the bytes from the pdf document, unkown class " + data.getClass().getName());
                          }
              
                          //Send email with attachement
                          Map<String,Object> emailMap = new HashMap<String,Object>();
                          emailMap.put("clientUser", clientUser);
                          emailService.sendMessageToUser(Email.client_usage_report, clientUser, emailMap);
              
                     }else{
                          log.error("No document found");
                     }
              
                }catch(Exception ex){
                     log.error("Error when trying to get the content of the excel in bytes with the message:\n {0}", ExceptionUtils.getStackTrace(ex));
                }finally{
                     emptyFacesContext.restore();
                }