Seam now includes an component set for generating documents using iText. The primary focus of Seam's iText document support is for the generation of PDF doucuments, but Seam also offers basic support for RTF document generation.
iText support is provided by jboss-seam-pdf.jar. This JAR contains the iText JSF controls, which are used to construct views that can render to PDF, and the DocumentStore component, which serves the rendered documents to the user. To include PDF support in your application, included jboss-seam-pdf.jar in your WEB-INF/lib directory along with the iText JAR file. There is no further configuration needed to use Seam's iText support.
The Seam iText module requires the use of Facelets as the view technology. Future versions of the library may also support the use of JSP. Additionally, it requires the use of the seam-ui package.
The examples/itext project contains an example of the PDF support in action. It demonstrates proper deployment packaging, and it contains a number examples that demonstrate the key PDF generation features current supported.
Documents are generated by facelets documents using tags in the http://jboss.com/products/seam/pdf namespace. Documents should always have the document tag at the root of the document. The document tag prepares Seam to generate a document into the DocumentStore and renders an HTML redirect to that stored content. The following is a a small PDF document consisting only a single line of text:
<p:document xmlns:p="http://jboss.com/products/seam/pdf"> The document goes here. </p:document>
The p:document tag supports the following attributes:
The type of the document to be produced. Valid values are PDF, RTF and HTML modes. Seam defaults to PDF generation, and many of the features only work correctly when generating PDF documents.
The size of the page to be generate. The most commonly used values would be LETTER and A4. A full list of supported pages sizes can be found in com.lowagie.text.PageSize class. Alternatively, pageSize can provide the width and height of the page directly. The value "612 792", for example, is equizalent to the LETTER page size.
The orientation of the page. Valid values are portrait and landscape. In landscape mode, the height and width page size values are reversed.
The left, right, top and bottom margin values.
Indicates that margin settings should be reversed an alternating pages.
Document metadata is also set as attributes of the document tag. The following metadata fields are supported:
Useful documents will need to contain more than just text; however, the standard UI components are geared towards HTML generation and are not useful for generating PDF content. Instead, Seam provides a special UI components for generating suitable PDF content. Tags like <p:image> and <p:paragraph> are the basic foundations of simple documents. Tags like <p:font> provide style information to all the content surrounging them.
<p:document xmlns:p="http://jboss.com/products/seam/pdf"> <p:image alignment="right" wrap="true" resource="/logo.jpg" /> <p:font size="24"> <p:paragraph spacingAfter="50">My First Document</p:paragraph> </p:font> <p:paragraph alignment="justify"> This is a simple document. It isn't very fancy. </p:paragraph> </p:document>
Most uses of text should be sectioned into paragraphs so that text fragments can be flowed, formatted and styled in logical groups.
The blank space to be inserted before the element.
The blank space to be inserted after the element.
The text tag allows text fragments to be produced from application data using normal JSF converter mechanisms. It is very similar to the outputText tag used when rendering HTML documents. Here is an example:
<p:paragraph> The item costs <p:text value="#{product.price}"> <f:convertNumber type="currency" currencySymbol="$"/> </p:text> </p:paragraph>
The value to be displayed. This will typically be a value binding expression.
Font declarations have no direct
The font family. One of: COURIER, HELVETICA, TIMES-ROMAN, SYMBOL or ZAPFDINGBATS.
The point size of the font.
The font styles. Any combination of : NORMAL, BOLD, ITALIC, OBLIQUE, UNDERLINE, LINE-THROUGH
p:image inserts an image into the document. Images can be be loaded from the classpath or from the web application context using the resource attribute.
<p:image resource="/jboss.jpg" />
Resources can also be dynamically generated by application code. The imageData attribute can specify a value binding expression whose value is a java.awt.Image object.
<p:image imageData="#{images.chart}" />
The location of the image resource to be included. Resources should be relative to the document root of the web application.
A method expression binding to an application-generated image.
The rotation of the image in degrees.
The height of the image.
The width of the image.
The alignment of the image. (see Section 15.8.2, “Alignment Values” for possible values)
Alternative text representation for the image.
The blank space to be inserted before the element.
The blank space to be inserted after the element.
The scaling factor (as a percentage) to use for the image. This can be expressed as a single percentage value or as two percentage values representing separate x and y scaling percentages.
p:anchor defines clickable links from a document. It supports the following attributes:
The name of an in-document anchor destination.
The destination the link refers to. Links to other points in the document should begin with a "#". For example, "#link1" to refer to an anchor postion with a name of link1. Links may also be a full URL to point to a resource outside of the document.
The p:header and p:footer components provide the ability to place header and footer text on each page of a generated document, with the exception of the first page. Header and footer declarations should appear near the top of a document.
The alignment of the header/footer box section. (see Section 15.8.2, “Alignment Values” for alignment values)
The background color of the header/footer box. (see Section 15.8.1, “Color Values” for color values)
The border color of the header/footer box. Individual border sides can be set using borderColorLeft, borderColorRight, borderColorTop and borderColorBottom.(see Section 15.8.1, “Color Values” for color values)
The width of the border. Inidvidual border sides can be specified using borderWidthLeft, borderWidthRight, borderWidthTop and borderWidthBottom.
If the generated document follows a book/article structure, the p:chapter and p:section tags can be used to provide the necessary structure. Sections can only be used inside of chapters, but they may be nested arbitrarily deep. Most PDF viewers provide easy navigation between chapters and sections in a document.
<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>
The chapter number. Every chapter should be assigned a chapter number.
The depth of numbering for section. All sections are numbered relative to their surrounding chapter/sections. The fourth section of of the first section of chapter three would be section 3.1.4, if displayed at the default number depth of three. To omit the chapter number, a number depth of 2 should be used. In that case, the section number would be displayed as 1.4.
List structures can be displayed using the p:list and p:listItem tags. Lists may contain arbitrarily-nested sublists. List items may not be used outside of a list. he following document uses the ui:repeat tag to to display a list of values retrieved from a Seam component.
<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>
p:list supports the following attributes:
The ordering/bulleting style of list. One of: NUMBERED, LETTERED, GREEK, ROMAN, ZAPFDINGBATS, ZAPFDINGBATS_NUMBER. If no style is given, the list items are bulleted.
For bulleted lists, specifies the bullet symbol.
The indentation level of the list.
For list styles using letters, indicates whether the letters should be lower case.
For ZAPFDINGBATS, indicates the character code of the bullet character.
For ZAPFDINGBATS_NUMBER, indicates the numbering style.
p:listItem supports the following attributes:
The alignment of the list item. (See Section 15.8.2, “Alignment Values” for possible values)
The left indentation amount.
The right indentation amount.
Overrides the default list symbol for this list item.
Table structures can be created using the p:table and p:cell tags. Unlike many table structures, there is no explicit row declaration. If a table has 3 columns, then every 3 cells will automatically form a row. Header and footer rows can be declared, and the headers and footers will be repeated in the event a table structure spans multiple pages.
<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>
p:table supports the following attributes.
The number of columns (cells) that make up a table row.
The relative widths of each column. There should be one value for each column. For example: widths="2 1 1" would indicate that there are 3 columns and the first column should be twice the size of the second and third column.
The initial number of rows which are considered to be headers or footer rows and should be repeated if the table spans multiple pages.
The number of rows that are considered to be footer rows. This value is subtracted from the headerRows value. If document has 2 rows which make up the header and one row that makes up the footer, headerRows should be set to 3 and footerRows should be set to 1
The percentage of the page width that the table spans.
The horizontal alignment of the table. (See Section 15.8.2, “Alignment Values” for possible values)
The blank space to be inserted before the element.
The blank space to be inserted after the element.
p:cell supports the following attributes.
Cells can span more than one column by declaring a colspan greater than 1. Tables do not have the ability to span across multiple rows.
The horizontal alignment of the cell. (see Section 15.8.2, “Alignment Values” for possible values)
The vertical alignment of the cell. (see Section 15.8.2, “Alignment Values” for possible values)
Padding on a given side can also be specified using paddingLeft, paddingRight, paddingTop and paddingBottom.
This section documents some of the constants shared by attributes on multiple tags.
Seam documents do not yet support a full color specification. Currently, only named colors are supported. They are: white, gray, lightgray, darkgray, black, red, pink, yellow, green, magenta, cyan and blue.
Document generation works out of the box with no additional configuration needed. However, there are a few points of configuration that are needed for more serious applications.
The default implementation serves PDF documents from a generic URL, /seam-doc.seam. Many browsers (and users) would prefer to see URLs that contain the actual PDF name like /myDocument.pdf. This capability requires some configuration. To serve PDF files, all *.pdf resources should be mapped to the Seam Servlet Filter, as shown here:
<filter> <filter-name>Seam Servlet Filter</filter-name> <filter-class>org.jboss.seam.servlet.SeamServletFilter</filter-class> </filter> <filter-mapping> <filter-name>Seam Servlet Filter</filter-name> <url-pattern>*.pdf</url-pattern> </filter-mapping>
The useExtensions option on the document store component completes the functionality by instructing the document store to generate URLs with the correct filename extension for the document type being generated.
<components xmlns="http://jboss.com/products/seam/components" xmlns:pdf="http://jboss.com/products/seam/pdf"> <pdf:documentStore useExtensions="true" /> </components>
Generated documents are stored in conversation scope and will expire when the conversation ends. At that point, references to the document will be invalid. To You can specify a default view to be shown when a document does not exist using the errorPage property of the documentStore.
<pdf:documentStore useExtensions="true" errorPage="/pdfMissing.seam" />