Priyanka Naik
Priyanka Naik

Reputation: 344

How to open PDF file in new window in browser?

I'm trying to open a PDF file by requesting it from Rest endpoint. But in this case, the file is getting downloaded. I tried opening it in another window. There also it just shows new window and downloads it there.

I used Chrome. So in Chrome by default it downloads the file while in Firefox it shows the dialog box whether to open it or save it. I don't want that dialog box. But want to display the file in new window with all the features like download, print, etc which a normal pdf viewer will show.

Is there some way through which I can avoid downloading of the file by default and just display that file in another window? Content-Disposition is attachment; filename="abc.pdf" when I see the properties of the URL. Also, its content-type is application/pdf;charset=utf-8.

<a target="_blank" data-content-type="application/pdf" onclick="open(this.href, this.target, 'fullscreen=yes'); return false;" data-type="downloadTenPointDocument" href="<c:out value="${resultItem.offer.programInfoUrl}"/>">View 10-point</a>

Upvotes: 1

Views: 7065

Answers (4)

FelipeFelix
FelipeFelix

Reputation: 11

i did like that and it worked: (it wont start to download, and it will open in a new tab (target _blank) if same tab, thenit is (target _self)

<a href="<c:url value='resources/pdfs/your pdf(already in the project).pdf' />" target="_blank"> show pdf in a new tab</a>

Upvotes: 0

Priyanka Naik
Priyanka Naik

Reputation: 344

I changed the properties of pdf file from attachment to inline and set it in response:

response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "inline; filename=" + fileName + ".pdf");

I am requesting the pdf file from server and then reading it and setting it in response, changing the properties of it in response, so that its default behavior of getting downloaded is overwritten.

Thank you everyone. The answer to change the property of pdf file was right.

Upvotes: 1

eWizard
eWizard

Reputation: 91

You should try to erase attachment; property from content disposition header.

Upvotes: 0

jarst
jarst

Reputation: 157

You won't be able to do it directly from REST endpoint. Use target="_blank" in anchor element on your page.

<a href="url_to_your_endpoint_generating_file.pdf" target="_blank">go</a>

Upvotes: 0

Related Questions