I want to open a pdf from an url in a new tab, so i used the usual window.open(url, '_blank'), but when i do, it open a new tab and download the PDF. How to force the navigator to display the PDF (in a new tab) instead of downloading ?
Asked
Active
Viewed 965 times
-3
-
Have you tried adding http header `content-disposition: inline`? https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition – Kristian Jun 07 '22 at 06:33
-
We can't add a header with window.open – Boursomaster Jun 07 '22 at 06:34
-
do it from server side – Kristian Jun 07 '22 at 06:34
-
@Boursomaster ugh, he means for the mime type *.pdf – Michael Mano Jun 07 '22 at 06:35
-
We don't have access to the server side – Boursomaster Jun 07 '22 at 06:35
-
1Make a page and iframe it if you really want to control what the user does with their own browser. – Michael Mano Jun 07 '22 at 06:36
-
Does this answer your question? [Opening a new tab to read a PDF file](https://stackoverflow.com/questions/16925481/opening-a-new-tab-to-read-a-pdf-file) – Yogi Jun 07 '22 at 06:40
-
That's what i do with window.open(url, '_blank'); The problem is that the url download the pdf – Boursomaster Jun 07 '22 at 06:52
1 Answers
0
The solution i found is to download the PDF, convert it a as a blob, then convert that blob into a new URL, then open it with window.open.
fetch(url, {/* YOUR CUSTOM HEADER*/ })
.then((response) => response.blob())
.then((blob) => {
const _url = window.URL.createObjectURL(blob);
window.open(_url, '_blank');
}).catch((err) => {
console.log(err);
});

Boursomaster
- 202
- 1
- 5
- 15