drenda
drenda

Reputation: 6244

Save pdf on disk using JasperReportsViewResolver with Spring 4.x

I'm using Spring Boot, Spring Data REST, Jasper Report (6.x). I created a REST controller that should export a PDF report on disk and return a "ok" string to the client. So, it's a bit different from the usual use case in which the user what the PDF is sent back to the client.

According to best practice, I'm using the solution 4 of this reply: https://stackoverflow.com/a/27532493/2012635 for the "normal" use case in which the PDF is returned to the client:

        @RequestMapping(value = "/refunds/{id}/export", method = RequestMethod.GET)
    public ModelAndView exportPdf(@PathVariable("id") Long id, HttpServletRequest request, Locale locale) throws Exception {
        Refund refund = refundRepository.findOne(id);
        if (refund != null) {
            ModelMap model = new ModelMap();
            model.addAttribute("datasource", new JREmptyDataSource(1));
            model.addAttribute("model", refund);        

            return new ModelAndView("RefundPdfView-IT", model);
        } else {
            throw new ResourceNotFoundException();
        }
    }

This approach is very clean, I've my mapping in the property file:

#REFUND
RefundPdfView-IT.(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
RefundPdfView-IT.url=classpath:/reports/accounting/refunds/Refund-IT.jrxml

I'm wondering if I can reuse this approach to save the PDF on the disk in the server rather than send it back to the client. I would like to reuse the mapping defined without hardcoding the position and names of reports.

Some advice would be appreciated.

Upvotes: 1

Views: 939

Answers (1)

constantine stanley
constantine stanley

Reputation: 11

First thing I would like to point out is you will need JasperExportManager. exportReportToPdfFile(JasperPrint jasperPrint, java.lang.String destFileName)  to save a file locally in the server.

From your use case it is not clear why you want to save the file in server, I feel instead of saving the file you should save the search parameters. so that when you click on the parameters you will get/generate the pdf file again.

or alternatively you should use curl in the server and call the required url with parameters

Upvotes: 1

Related Questions