Using PDF Views

Even though the previously discussed view technologies are powerful, they still depend on the browser to interpret the generated HTML code. Even though basic HTML renders without any problems on all browsers, you cannot guarantee that a page with complex formatting is going to look the same on all browsers. What's more, sometimes HTML is not suitable if you need to print the output, or use a particularly complex formatting. If this is the case, your best solution is to use a document format that renders consistently on all clients. For this, you need to include the iText (www.lowagie.com/iText/) JAR file, which is already included in the Spring distribution.

Unfortunately, there is no PDF template language, so we have to implement the views ourselves. As you saw in Chapter 17, implementing a custom view is not a difficult task, and Spring provides a convenience superclass, AbstractPdfView, which you can use in your PDF view implementation.

Implementing a PDF View

The PDF view we implement in this section displays product details. We do not change the ProductController whose view() method processes the requests to view a product, but we do need to change to the views.properties file and implement the ProductPdfView class to create the PDF output.

Let us begin by looking at Listing 18-20, which shows the implementation of the ProductPdfView class using the iText library.

Listing 18-20: ProductPdfView Implementation

image from book
package com.apress.prospring.ch18.web.views;      import java.util.Map;      import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;      import org.springframework.web.servlet.view.document.AbstractPdfView;      import com.apress.prospring.ch18.domain.Product; import com.lowagie.text.Document; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter;      public class ProductPdfView extends AbstractPdfView {          protected void buildPdfDocument(Map model, Document document,          PdfWriter writer, HttpServletRequest request,          HttpServletResponse response) throws Exception {         Product product = (Product) model.get("product");         if (product == null) throw new              NullPointerException("Product not present in the model");              Paragraph header = new Paragraph("Product details");         header.font().setSize(20);         document.add(header);                  Paragraph content = new Paragraph(product.getName());         document.add(content);                  Paragraph footer = new Paragraph("Pro Spring Chapter 18");         footer.setAlignment(Paragraph.ALIGN_BOTTOM);         document.add(footer);     }      }
image from book

This view simply extracts the Product domain object from the model and uses its data to add Paragraphs to the document instance. Because the ProductPdfView is a subclass of AbstractPdfView, we do not have to take care of setting the appropriate HTTP headers or performing any I/O—the superclass takes care of all that.

Before we can verify that the newly created ProductPdfView class works, we need to modify the views.properties file, make sure that the product-view's class is set to ProductPdfView, and make sure that the ProductController.view() adds a Product instance to the model. Listing 18-21 shows the changes we need to make to the views.properties file.

Listing 18-21: views.properties File for product-view

image from book
product-view.class=com.apress.prospring.ch18.web.views.ProductPdfView
image from book

You may notice that we do not need to set any additional properties on product-view. Next, in Listing 18-22, we make sure that the ProductController.view() method returns the correct instance of ModelAndView.

Listing 18-22: ProductController.view Implementation

image from book
public class ProductController extends MultiActionController {     private Product createProduct(int productId, String name,          Date expirationDate) {         Product product = new Product();         product.setProductId(productId);         product.setName(name);         product.setExpirationDate(expirationDate);                  return product;     }          public ModelAndView view(HttpServletRequest request,          HttpServletResponse response) throws Exception {         Product product = createProduct(1, "Pro Spring", new Date());         return new ModelAndView("product-view", "product", product);     } }
image from book

When the application is rebuilt and deployed, we should see the PDF document in Figure 18-3 returned for /product/view.html.

image from book
Figure 18-3: ProductPdfView output

Generating a PDF from your Spring web application is not too difficult, even though a lot of coding is involved.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net