Reputation: 993
I am generating a pdf with japser reports and I would like to create a REST web service that will return this pdf and display it in the browser. I have already tried the code displayed here:
REST web services method to display pdf file in browser
But in this way the pdf file is downloaded. I would prefer it to be displayed in the browser first and then if the user wants he could download it later.
(sorry for the duplicate question, but as you can see the above question has not been answered... )
EDIT:
Working REST Service Code:
@GET
@Path("/pdf")
@Produces("application/pdf")
public javax.ws.rs.core.Response getPdf() throws Exception
{
File file = new File("E:\\tmp\\test.pdf");
FileInputStream fileInputStream = new FileInputStream(file);
javax.ws.rs.core.Response.ResponseBuilder responseBuilder = javax.ws.rs.core.Response.ok((Object) fileInputStream);
responseBuilder.type("application/pdf");
responseBuilder.header("Content-Disposition", "filename=test.pdf");
return responseBuilder.build();
}
Upvotes: 29
Views: 61606
Reputation: 374
Using InputStream first we can load the file into inputstream, then pass to IOUtils.copy and use this response.header("Content-Disposition", "attachment; filename=restfile.pdf")
for download, use this for preview response.setHeader("Content-disposition", " filename=" + output)
InputStream inputStream = new FileInputStream(new File(path)); // load the file
IOUtils.copy(inputStream, response.getOutputStream());
response.setContentType("application/pdf");
response.setHeader("Content-disposition", " filename=" + output);
response.flushBuffer();
Return type of the method is `ResponseEntity
Upvotes: 5
Reputation: 1
Following the example of @Branislav Lazic, the way i found is to @Autowired
the JasperReportsViewResolver
in your report controller:
@Autowired
private JasperReportsViewResolver jrvr;
And override (or update) the headder propertie "Content-Disposition"
. For example:
...
final Properties properties = new Properties();
properties.setProperty("Content-Disposition", "attachment; filename=newReportName.xlsx");
jrvr.setHeaders(properties);
...
I don´t know if its the best way but works fine for me.
Upvotes: 0
Reputation: 14806
You can easy do that with JasperReportsViewResolver. Presuming that you have data source defined, create a bean definition like this:
@Autowired
private DataSource dataSource;
@Bean
public JasperReportsViewResolver getJasperReportsViewResolver() {
JasperReportsViewResolver resolver = new JasperReportsViewResolver();
resolver.setPrefix("classpath:/reports/");
resolver.setSuffix(".jrxml");
resolver.setJdbcDataSource(dataSource);
resolver.setViewNames("rpt_*");
resolver.setViewClass(JasperReportsMultiFormatView.class);
resolver.setOrder(0);
return resolver;
}
Your reports will be in i.e. /reports/
directory and your reports will start with prefix rpt_
. So in reports
folder, you will put your JRXML
files.
And your request mapping method will look like this i.e.:
@RequestMapping(value = "/report", method = RequestMethod.GET)
public ModelAndView showReport() {
ModelMap modelMap = new ModelMap();
modelMap.put("format", "pdf");
ModelAndView modelAndView = new ModelAndView("rpt_myReport", modelMap);
return modelAndView;
}
You can basically threat your report like just another view. File will be opened in new tab with ability to download it, print it etc.
Upvotes: 0
Reputation: 2922
change
response.header("Content-Disposition", "attachment; filename=restfile.pdf");
to
response.header("Content-Disposition", "filename=restfile.pdf");
Upvotes: 35