Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a PDF file with Spring MVC

Actually, I have that functionality, I got a frame where I set the URL (ip:port/birt/preview?__report=report.rptdesign&__format=pdf&parameters...) and that frame renders the PDF file.

But I want that URL hidden...

I need return a PDF file with Spring MVC but that PDF is generated by another application.

This means I got another app (Eclipse Birt Engine) that I pass the parameters through of an URL (ip:port/birt/preview?__report=report.rptdesign&__format=pdf&parameters...) and it generates a PDF file, I need from my controller get that PDF and return it with Spring MVC. Could somebody help?

like image 675
Fernando Pineda Avatar asked Dec 10 '14 16:12

Fernando Pineda


Video Answer


1 Answers

That would be like the below:

@Controller
@RequestMapping("/generateReport.do")
public class ReportController

    @RequestMapping(method = RequestMethod.POST)
    public void generateReport(HttpServletResponse response) throws Exception {

        byte[] data = //read PDF as byte stream

        streamReport(response, data, "my_report.pdf"));
    }

    protected void streamReport(HttpServletResponse response, byte[] data, String name)
            throws IOException {

        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "attachment; filename=" + name);
        response.setContentLength(data.length);

        response.getOutputStream().write(data);
        response.getOutputStream().flush();
    }
}
like image 57
Alan Hay Avatar answered Oct 16 '22 15:10

Alan Hay