PDF views in Seam

Posted by    |       Seam

In Seam 1.1.1 a PDF view is just like any other page in the application: a facelets template containing EL expressions that bind values from the underlying Seam components onto the page. So if you already know Seam, producing PDF just involves learning the new iText-specific tags.

Here's a simple Hello example in Seam PDF:

<p:document [=>xmlns:p=]"=>http://jboss.com/products/seam/pdf"
            title="Hello">
   <p:paragraph>Hello #{user.name}!</p:paragraph>
</p:document>

Just throw this document in your docroot directory of a Seam project, and access the page like you would any other facelets page. What could be easier?

For larger documents, we'll probably want to break our document up into chapters:

<p:document [=>xmlns:p=]"=>http://jboss.com/products/seam/pdf"
            title="Hello">

   <p:chapter number="1">
      <p:title><p:paragraph>Hello</p:paragraph></p:title>
      <p:paragraph>Hello #{user.name}!</p:paragraph>
   </p:chapter>

   <p:chapter number="2">
      <p:title><p:paragraph>Goodbye</p:paragraph></p:title>
      <p:paragraph>Goodbye #{user.name}.</p:paragraph>
   </p:chapter>

</p:document>

Of course, the real usefulness of this feature is for reporting, so I bet you want to know how to render dynamic lists and tables. Well, here's a list:

<p:document [=>xmlns:p=]"=>http://jboss.com/products/seam/pdf"
            [=>xmlns:ui=]"=>http://java.sun.com/jsf/facelets"
            title="Hello">
   <p:list style="numbered">
      <[=>ui:repeat] value="#{documents}" var="doc">
         <p:listItem>#{doc.name}</p:listItem>
      </[=>ui:repeat]>
   </p:list>
</p:document>

And here's a table:

<p:document [=>xmlns:p=]"=>http://jboss.com/products/seam/pdf"   
            [=>xmlns:ui=]"=>http://java.sun.com/jsf/facelets"
            title="Hello">   
   <p:table columns="3" headerRows="1">
      <p:cell>name</p:cell>
      <p:cell>owner</p:cell>
      <p:cell>size</p:cell>
      <[=>ui:repeat] value="#{documents}" var="doc">
         <p:cell>#{doc.name}</p:cell>
         <p:cell>#{doc.user.name}</p:cell>
         <p:cell>#{doc.size}</p:cell>
      </[=>ui:repeat]>
   </p:table>
</p:document>

Of course, there's much more functionality available: sections, anchors, fonts, etc, but that should be enough to give you the flavor of this new stuff. Check out the examples in Seam 1.1.1 for more information.


Back to top