Display PDF using Spring MVC - bahkified/Notes GitHub Wiki
There may be situations where you want to render a particular URL as a PDF, say you display an invoice. You want the user to be able to save and/or print this invoice, so PDF makes for a natural format.
##Dependencies Besides the usual Spring dependencies, you need to be able to handle PDFs in Java. The iText library is a common one, although only the older versions are free to use. The newer versions of iText have different structure and different licensing schemes. Use the following Maven coordinates:
<!-- Pdf library -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
##The View class
In your Spring MVC controller, include a @RequestMapping
for your desired URL. I usually use something resembling:
@RequestMapping(value = "asPdf", method = RequestMethod.GET)
public String getViewAsPdf(Model model) {
// put stuff in your model
return "pdfView";
}
Now you need to define a View class that will actually build the PDF document. This class needs to overwrite AbstractPdfView
which is already defined in Spring.
public class PdfView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, String> revenueData = new HashMap<String, String>();
revenueData.put("1/20/2010", "$100,000");
revenueData.put("1/21/2010", "$200,000");
revenueData.put("1/22/2010", "$300,000");
revenueData.put("1/23/2010", "$400,000");
revenueData.put("1/24/2010", "$500,000");
Table table = new Table(2);
table.addCell("Month");
table.addCell("Revenue");
for (Map.Entry<String, String> entry : revenueData.entrySet()) {
table.addCell(entry.getKey());
table.addCell(entry.getValue());
}
document.add(table);
}
}
##Configure Spring
In your Spring MVC configuration, define a view resolver for this pdf view. It will be an XmlViewResolver. The order
property set to zero means that this view resolver will look for resources first.
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location" value="classpath:pdf-view.xml"/>
<property name="order" value="0"/>
</bean>
There is a location
property here, which tells the XmlViewResolver where to look for the PDF view classes. This is a separate <beans>
configuration file. It must define a bean for the PdfView class that you created earlier. When your Spring MVC controller specifies a view name, the XmlViewResolver will look for a bean with an ID of the same name and it will use the class associated with it.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="pdfView" class="my.package.PdfView"/>
</beans>
You should be good to go now. Load up the target URL in your browser to view the PDF!
##Resources