Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display pdf in browser using a rest service

Tags:

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(); } 
like image 702
jenny Avatar asked Aug 25 '15 09:08

jenny


People also ask

How do I show a PDF on my website?

Navigate to the "Open With" option and choose "Chrome PDF Viewer" from the drop-down menu. You can also drag a PDF document directly into the browser, and it will open. Using this above outline method, opening a PDF document becomes easy. You can view a downloaded document directly using this method.

How do I control how PDFs open from a web page?

Each browser has its own settings to control how PDFs open from a web page. Acrobat and Acrobat Reader do not include a preference setting to open web-based PDFs. To change the display behavior, follow the instructions below for your browser, or see the browser documentation on how to control plug-ins or add-ons.

How to open a PDF file in another browser using API?

Normally, the API would provide a json-object with a link to the pdf, which can then be used to open a new browser tab/window and the browser or platform takes care of the rest.

How to preview a PDF file in Firefox?

Step 1: Launch the Firefox browser. Navigate to the "Options" button and choose "Applications" on the top toolbar. Step 2: You can find the option "Portable Document Format (PDF)" window. From the list that shows up, select "Preview in Firefox".

How do I enable Adobe PDF reader in Internet Explorer?

Make sure that the Adobe PDF browser add-on is enabled. Open Internet Explorer and choose Tools > Manage Add-ons. Under Add-on Types, select Toolbars and Extensions. In the Show menu, choose All add-ons. In the list of add-ons, select Adobe PDF Reader.


2 Answers

change

response.header("Content-Disposition",  "attachment; filename=restfile.pdf"); 

to

response.header("Content-Disposition",  "filename=restfile.pdf"); 
like image 53
Saurabh Jhunjhunwala Avatar answered Sep 23 '22 19:09

Saurabh Jhunjhunwala


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

like image 32
chandrashekar.n Avatar answered Sep 20 '22 19:09

chandrashekar.n