0

I have a Spring MVC webapp that generates reports using JSP/JSTL. The client is now asking that I produce a link to PDF versions of the reports, which will have slightly different layout (eg. lose pagination, navigation etc).

In my servlet context I have:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
    <map>
      <entry key="pdf" value="application/pdf"/>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
  </property>
  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
</bean>

Which is taken from a tutorial somewhere. This seems to work as my old view resolver in that a GET with or without .html gets handled by my reportController (annotation request path mapping) and passed to the JSP and rendered correctly. The ReportController methods return a Strings that get resolved to JSPs.

Somehow I have to setup a PDF view that I can customise the layout of, perhaps using CSS or XSLT.

So right now a GET request to reports/basic/ or reports/basic.html will return the JSP rendering, I need to be able to do GET requests to fetch a PDF on URLs like reports/basic.pdf

DaFoot
  • 1,475
  • 8
  • 29
  • 58

1 Answers1

2

The creation of an PDF is out of the scope of Spring itself.

May you should have a look at this Stack Overflow Question: Using itext to convert HTML to PDF. It discuss how to create PDFs from an HTML page, for example with IText, Pd4mL.

An other way is discussed here. It is based on XSTL.

Of course, you can integrate your solution in your Spring Application, but Spring will give you not so much extra support for that task.

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383