Display PDF in Spring example 2016-05-24 22:16

In this tutorial I will show you how to display and download pdf file in Spring(Spring MVC). For quick beginning we use Spring Boot, and for displaying pdf we use jsp as view.(How to use jsp in Spring Boot, click Spring Boot JSP examples).

Project structure

│  ├─java
│  │  └─com
│  │      └─henryxi
│  │          └─pdf
│  │                  DisplayDownloadPDFController.java
│  │
│  └─resources
│      │  application.properties
│      │
│      └─META-INF
│          └─resources
│              │  Accepted.pdf
│              │
│              └─WEB-INF
│                  └─jsp
│                          index.jsp

pom file


Java code

public class DisplayDownloadPDFController{

    public String index() {
        return "index";

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void getFile(HttpServletResponse response) {
        try {
            DefaultResourceLoader loader = new DefaultResourceLoader();
            InputStream is = loader.getResource("classpath:META-INF/resources/Accepted.pdf").getInputStream();
            IOUtils.copy(is, response.getOutputStream());
            response.setHeader("Content-Disposition", "attachment; filename=Accepted.pdf");
        } catch (IOException ex) {
            throw new RuntimeException("IOError writing file to output stream");

    public static void main(String[] args) throws Exception {
        SpringApplication.run(DisplayDownloadPDFController.class, args);


server.port = 8090


<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
<embed src="/Accepted.pdf" width="500" height="375" type='application/pdf'>
<a href="/download">download</a>

Run main method in DisplayDownloadPDFController and access http://localhost:8090/. You can see the pdf content and download link. This example works both in chrome and firefox.