1 2 Previous Next 26 Replies Latest reply on Jun 13, 2011 6:13 PM by Miriam Choque

    PDF-documentStore

    Werner Jacobs Newbie

      Hi everyone,


      For the moment we are rendering a mail message and attaching a pdf to the mail. We would rather like to store this pdf.
      How can I access the pdf data?
      Is there a solution to retrieve the document out of the documentStore via Contexts? Or is there an other way to achieve this?


      Thx,


      Werner

        • 1. Re: PDF-documentStore
          Nicklas Karlsson Master

          Jacobs Werner wrote on Feb 20, 2008 08:16 AM:

          documentStore via Contexts? Or is there an other way to achieve this?


          I have found that databases are really good for storing stuff in ;-)

          • 2. Re: PDF-documentStore
            Werner Jacobs Newbie

            Hi Nicklas,


            My problem is not so much where to store the pdf. But more how do I retrieve the pdf data after rendering my mail or even without rendering at all.


            If I do not misunderstand seam doc, they store the pdf temporary in the DocumentStore.(org.jboss.seam.pdf.DocumentStore, conversation scoped built-in component).
            I would like to access this DocumentStore via Contexts.


            thx,


            Werner


            • 3. Re: PDF-documentStore
              Nicklas Karlsson Master

              There used to be a DocumentStore.instance()... does it give you not null?

              • 4. Re: PDF-documentStore
                Werner Jacobs Newbie

                Thanks for the response,it gives me nothing (no documentData) in the documentStore so I still do not manage.


                I am looking now into a solution they proposed in the
                following Seam JIRA issue and its reference to another item in Jboss Forum.

                • 5. Re: PDF-documentStore
                  Ben Catherall Newbie

                  There is a way to get the data from the conversation context, without having to change anything:


                  First you need to create a temporary FacesContext so that the PDF redirect doesn't propagate.


                  public class EmptyFacesContext extends MockFacesContext {
                  
                      FacesContext originalFacesContext;
                  
                      public EmptyFacesContext(){
                          super(new MockExternalContext());
                          originalFacesContext = FacesContext.getCurrentInstance();
                          createViewRoot();
                          FacesContext.setCurrentInstance(this);
                      }
                  
                      public void restore(){
                          setCurrentInstance(originalFacesContext);
                      }
                  
                  }



                  Then we render the PDF, and obtain the data by reflection (as the Seam PDF jar wont necessarily be accessible in your EJB classpath).


                  String DATA_STORE = "org.jboss.seam.pdf.documentStore.dataStore";
                  
                  EmptyFacesContext emptyFacesContext = new EmptyFacesContext();
                  
                  try {
                  
                      Renderer renderer = Renderer.instance();
                      renderer.render("/WEB-INF/pdfs/pdf.xhtml");
                  
                      byte[] bytes = null;
                  
                      Map<String,Object> dataStore = (Map<String, Object>) conversation.get(DATA_STORE);
                      if (dataStore != null){
                  
                          Collection documentDatas = dataStore.values();
                          if (!documentDatas.isEmpty()){
                              Object documentData = documentDatas.iterator().next();
                              if (documentData != null){
                                  Method method = Reflections.getGetterMethod(documentData.getClass(), "data");
                                  Object data = Reflections.invokeAndWrap(method, documentData);
                                  if (data instanceof byte[]) {
                                      bytes = (byte[]) data;
                                  }
                              }
                          }
                      }
                  
                  } finally {
                      emptyFacesContext.restore();
                  }




                  Then create your PDF facelet, and away you go.


                  Cheers- Ben

                  • 6. Re: PDF-documentStore
                    Ben Catherall Newbie

                    at the top of the second block above


                    Context conversation = Contexts.getConversationContext()

                    • 7. Re: PDF-documentStore
                      Maniappan  R Newbie

                      Hi Ben,
                      How do I resolve the MockFacesContext and MockExternalContext methods?
                      Mani.

                      • 8. Re: PDF-documentStore
                        Maniappan  R Newbie

                        Hi Ben,Forget my last request, posted early w/o checking the docs. - Mani.

                        • 9. Re: PDF-documentStore
                          Leo - Newbie

                          Hi! I have tested this approach to save pdf on server. It's very usefull.


                          Just say that when render is executed fails if you have commented code in xhtml.
                          Is there any way to solve this without removing comments?

                          • 10. Re: PDF-documentStore
                            Georges Berscheid Newbie

                            Try putting the following in your web.xml:


                                 <context-param>
                                      <param-name>facelets.SKIP_COMMENTS</param-name>
                                      <param-value>true</param-value>
                                 </context-param>
                            


                            That has solved a bunch of comment-related problems for me.


                            Cheers,
                            Georges

                            • 11. Re: PDF-documentStore
                              Shervin Asgari Master

                              The following code in the new Seam 2.1.2 does not compile because DocumentData no longer has getData(). It is moved to ByteArrayDocumentData.



                              EmptyFacesContext emptyFacesContext = new EmptyFacesContext();
                              
                              // Context conversation = Contexts.getConversationContext();
                              byte[] bytes = null;
                              Renderer.instance().render(path + "/status/status_pdf.xhtml");
                              DocumentStore doc = DocumentStore.instance();
                              if (doc != null) {
                              DocumentData data = doc.getDocumentData("1");
                              bytes = data.getData();
                              }



                              How can I get the data from the ByteArrayDocumentData object?

                              • 12. Re: PDF-documentStore
                                Shervin Asgari Master

                                Hmm, after further debugging I see that the data is actually automatically a ByteArrayDocumentData object. So I just needed to cast it and it worked

                                • 13. Re: PDF-documentStore
                                  Jorge Maroto Newbie

                                  Hi! I'm beginner on Seam, and I need to save a PDF on the server-side (for digital signature). I have been trying this sample, but I don't understand how I create the PDF facelet. I suppose that this is trivial for all of you, but I am disoriented with so many layers and technologies (Java, JSP, JSF, Seam...)



                                  Regards, and sorry my bad english ;)

                                  • 14. Re: PDF-documentStore
                                    Shervin Asgari Master
                                    If you don't understand the code above then you should read the Seam pdf documentation. In the code above path + "/status/status_pdf.xhtml" is a link to a seam pdf xhtml page.

                                    I don't know what more to say. It's pretty straight forward if you have a valid seam pdf xhtml page.
                                    1 2 Previous Next