Spring Mvc Reference Documentation Pdf

Active3 years, 1 month ago

How do I force the browser to display the pdf instead of downloading ? Here is the controller

Spring Mvc Reference Documentation Pdf

Nabil ShamNabil Sham

Built by the DocBook Framework pdf Adobe PDF format Spring is an example on how good documentation makes all the difference. There is a standart template on all 'Spring Reference Documentation's. Learn Spring. Here you'll find all the documentation and tutorials written by the Spring team. You can also generate a new Spring Boot project in seconds by clicking on the following button. Start a new Spring project. Read the Reference Documentation. Sagan Application.

9753 gold badges16 silver badges34 bronze badges

2 Answers

If you remove this line, the pdf will open in the browser itself.

sharmakeshavsharmakeshav
1921 gold badge3 silver badges13 bronze badges

It looks like the above mentioned controller is all we need from the server side, the problem is the browser doesn't support viewing PDF files.

Nabil ShamNabil Sham
9753 gold badges16 silver badges34 bronze badges

Not the answer you're looking for? Browse other questions tagged javaspring-mvcpdf or ask your own question.

In this post we’ll see how to generate a PDF in Spring MVC using the fields from a view page (JSP).

Technologies used

Spring Mvc Reference Documentation Pdf Pdf

Following is the list of tools used for the Spring MVC PDF generation example.

  • Spring 5.0.8 Release (Spring core, spring web, spring webmvc).
  • Java 10
  • Tomcat server V 9.0.10
  • Eclipse Photon 4.8.0 for Java EE development (This Eclipse version supports Java 10)
  • iText 7.1.3
  • OpenPDF 1.2.3

Options for generating PDF in Spring MVC

  1. One of the option for generating PDF is iText. In Spring framework iText support is for a very old version so requires some work on your behalf to get it working with the current versions.
  2. Another option is OpenPDF which is a fork from iText. Spring framework class AbstractPdfView can directly be used to generate PDF using OpenPDF. Spring framework recommends OpenPDF.
    Reference - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/view/document/AbstractPdfView.html

Spring MVC PDF generation example using iText

First we’ll see how to generate a PDF using iText.

PdfMaven dependencies

Maven is used for managing dependencies in this Spring MVC PDF generation example.

  • Please refer Spring Web MVC Example With Annotations to see how to set Spring MVC project using Maven.

In the pom.xml apart from Spring framework dependencies, iText dependencies are also added which adds the following jars.

Spring MVC PDF generation example flow

In the example there is a JSP that shows a list of Users and there is a button “ViewPDF”. In the JSP that list of users is bound to a Model.

When the button is clicked, the request is mapped to the appropriate controller method and from there the logical view name and Model where the list of users is set as attribute is transferred to the view which creates a PDF. To resolve a view to a PDF another view resolver has to be added.

Spring Mvc Book Pdf

Spring MVC PDF generation – iText related classes

With in Spring framework there is an abstract class AbstractPdfView which acts as a superclass for PDF views. But the AbstractPdfView class works with the original iText 2.1.7 version (with com.lowagie.* package) where as the current version is iText 7.x (with com.itextpdf package).

That requires some work on your behalf to make the Spring MVC work with the current iText version. AbstractPdfView class extends AbstractView class and adds PDF specific functionality. So, you need to do that yourself, creating a class that extends AbstractView and uses the new com.itextpdf package. You can still copy most of the functionality from the AbstractPdfView class, difference is now you are pointing to the com.itextpdf.* classes where as AbstractPdfView class uses the older com.lowagie.* classes. Also code for PDF generation in iText 7.x is a little different from iText 5.x version.

Now NetJSAbstractViewPDF is the abstract class which provides the PDF specific functionality and you need to extend this class to implement abstract method buildPdfDocument() as per your PDF document structure requirements.

Spring MVC PDF generation – Controller Class

In the Controller class there are two handler methods. Method getUsers() displays the list of users in a JSP page (showUsers.jsp). In that JSP there is a “View PDF” button, the request created on clicking that button is handled by the handler method viewPDF() which passes the list of users and the logical view name to be resolved to a PDF view.

Spring MVC PDF generation – Views

showUsers.jsp

This JSP displays the users in the List by iterating the List and again bind list to the Model. Clicking “View PDF” button generates the PDF using the List bound to the Model.

We have already seen the PDF view class PDFView.java.

Spring MVC PDF generation – Model Classes

List will have objects of the User class.

Following class acts a container for the List of User objects.

Spring MVC PDF generation – Configuration

The Spring configuration file is as follows.

mvcexample-servlet.xml

As you can see two view resolver classes are configured here.

ResourceBundleViewResolver resolves the views from the properties file. ResourceBundleViewResolver is the Implementation of ViewResolver that uses bean definitions in a ResourceBundle, specified by the bundle base name, and for each view it is supposed to resolve, it uses the value of the property [viewname].(class) as the view class and the value of the property [viewname].url as the view url.
Here “basename” property has the value pdf-view which means properties file is named pdf-view.properties file.

pdf-view.properties

Controller class handler method viewPDF returns logical view name as “viewPDF” which is used to resolve the view class using this properties file.

Another view resolver InternalResourceViewResolver is used to resolve the view name to JSPs.

Here note that both the Resolvers have a property order too which decides the priority. Lower order value means higher priority. Here ResourceBundleViewResolver has order set as 1 which means Spring framework will first try to resolve view using this class.

As a rule InternalResourceViewResolver should always have higher order value because it will always be resolved to view irrespective of value returned giving no chance to any other Resolver class.

Spring MVC PDF generation using OpenPDF

If you are using OpenPDF to generate PDF in your Spring MVC application then you need to make following changes.

Maven dependency

For OpenPDF you need to add following dependency in your pom.xml

PDF view class for OpenPDF

When using OpenPDF you can directly subclass AbstractPdfView as OpenPDF uses com.lowagie.* classes, no need to create your own Abstract class by extending AbstractView class.

Properties class change for OpenPDF

In properties class referred by ResourceBundleViewResolver, now the view class should be this new PDF view class.

Spring Mvc Reference Documentation Pdf Free

pdf-view.properties

Reference

Deploying and testing the application

Once the application is deployed to Tomcat server it can be accessed using the URL- http://localhost:8080/spring-mvc/getUsers

Generated PDF - Spring MVC PDF Generation Example

On clicking the View PDF button PDF is generated. Screen shot shows how it looks like in Chrome browser.

That's all for this topic Spring MVC PDF Generation Example. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

Spring Reference Pdf

You may also like -