11 Replies Latest reply on Feb 4, 2007 2:11 PM by Norman Richards

    Accessing generated PDFs

    Pete Muir Master

      So, at the moment you can generate PDFs by visiting /mypdf.seam, and then they are downloaded to you in your browser. But it would be great to be able to attach them to seam generated emails or perhaps get them in your backing bean (e.g. to send off to your fax gateway).

      Also there is soon going to be some jfreechart components. We want the images to be displayable in PDFs, but obviously that is exactly the type of thing you might want to include in an email (or even on screen directly) outside of a PDF.

      This, at least is how I understand the problem...

        • 1. Re: Accessing generated PDFs
          Pete Muir Master

          So, for emails, this is my suggestion as to how you should be able to attach PDFs to the email:

          <m:message>
           ...
           <m:attachment>
           <p:document>
           ...
           </p:document>
           </m:attachment>
          </m:message>


          Of curse with this you would be able to use all the standard facelets templating stuff (ui:include being the one that springs to mind).

          As for implementation I would guess something like:

          UIAttachment
          encodeBegin() {
           // If the attachment element contains a document element
           documentComponent.setRedirect(false);
          }
          
          encodeEnd() {
           // If the attachment element contains a document element
           InputStream is = documentComponent.getPdf();
           String name = documentComponent.getName();
           // Deal with attaching the InputStream
          }


          I write this tho' with no idea of the feasability of doing that with the jboss-seam-pdf ;)

          There are some other problems with embedding/attaching images to emails (generally you want to display them inline as part of your html body than attach them).

          • 2. Re: Accessing generated PDFs
            Norman Richards Master

            What about a new <p:embeddedDocument> tag? (ok - we need a better name) This would be a subclass of UIDocument and replace the <p:document> tag only performing PDF creation portion. Actually, if I create that component then you could also register it as <m:document> to provide a nice local name.

            • 3. Re: Accessing generated PDFs
              Norman Richards Master

              The alternative is to keep <p:document> as the component that builds a PDF, and require it to be surrounded by something else like <p:saveAndRedirect> or <m:attachment>. This would break existing PDF usage, but since the functionality is so new I don't think it would cause our early adopters too much grief to have to add a new tag.

              • 4. Re: Accessing generated PDFs
                Pete Muir Master

                Hmm - I like the idea of just being able to drop a p:document tag (via a ui:include) into a m:attachment tag, it's simple and requires the user to do no wiring. This way the same facelet could be used both in emails AND in the browser.

                So I'm -1 on p:embeddedDocument atm

                I prefer the p:saveAndRedirect approach but I guess my preferred route is to keep p:document, have it do a saveAndRedirect by default, unless told not to by a parent component.

                • 5. Re: Accessing generated PDFs
                  Norman Richards Master

                  Ok, UIDocument now has a sendRedirect flag. If this is set (either in the template or by the parent component reaching down) we will bypass the the generation and just keep the value locally.

                  We needed some way to transfer the data. I noticed UIAttachment us using get/setValue. Assuming that it implemented ValueHolder, I thought I might reach up and set the values directly if I could. Unfortunately, I just realized you don't actually implement ValueHolder. I'm not sure this is proper use of ValueHolder anyway, so if you have a better idea, I'm all ears.

                  Also, note that you'll need to do your processing in encodeEnd() and not in encodeBegin(). The PDF won't available until UIDocument completes it's encodeEnd.



                  • 6. Re: Accessing generated PDFs
                    Pete Muir Master

                     

                    "norman.richards@jboss.com" wrote:
                    Ok, UIDocument now has a sendRedirect flag. If this is set (either in the template or by the parent component reaching down) we will bypass the the generation and just keep the value locally.


                    cool :)

                    We needed some way to transfer the data. I noticed UIAttachment us using get/setValue. Assuming that it implemented ValueHolder, I thought I might reach up and set the values directly if I could. Unfortunately, I just realized you don't actually implement ValueHolder. I'm not sure this is proper use of ValueHolder anyway, so if you have a better idea, I'm all ears.


                    http://java.sun.com/javaee/javaserverfaces/1.1_01/docs/api/javax/faces/component/ValueHolder.html - I think that implementing ValueHolder here is a correct (I just hadn't) - there might be some interesting ways of using JSF Converters (though as they *have* to return a String when converting from the model to the view perhaps not)

                    • 7. Re: Accessing generated PDFs
                      Norman Richards Master

                      Let me know if you need anything else to make this work.

                      • 8. Re: Accessing generated PDFs
                        Pete Muir Master

                        Could there be a public accessor for docType to UIDocument so I can set the contentType for the attachment.

                        Also for baseName so that I can set the filename of the attachment

                        • 9. Re: Accessing generated PDFs
                          Norman Richards Master

                          org.jboss.seam.pdf.DocumentStore has an internal DocumentData class. I could move this out and use that structure as what I pass you, instead of just a byte[]. Do you think that would work better?

                          • 10. Re: Accessing generated PDFs
                            Pete Muir Master

                            That's probably neater.

                            • 11. Re: Accessing generated PDFs
                              Norman Richards Master

                              Ok - done. You are now getting an org.jboss.seam.pdf.DocumentData.