I am trying to open up this string in a new window as a PDF file. Whenever I use window.open() and write the string to the new tab it thinks that the text should be the contents of an HTML document. I want it to recognize that this is a PDF file. Any help is much appreciated
asked May 10, 2010 at 18:26 1,049 2 2 gold badges 9 9 silver badges 6 6 bronze badgesHow are you "writing" this string? Do you mean it's returned by the backend (if so, what platform?). If the PDF data is being generated by the backend, make sure you set the correct Content-Type header.
Commented May 10, 2010 at 18:37 Best answer: You should not be using AJAX in this situation. Commented May 10, 2010 at 18:43@Josh - He never mentioned AJAX. It could be a standalone HTML page on a CD dynamically generating customised strings for all we know. I doubt that this is possible though.
Commented May 10, 2010 at 18:52It is being created on the backend but it will not be possible/elegant to save on the server. Furthermore, content is already being written to the page before the pdf is being generated. thus setting a header to pdf will not work.
Commented May 10, 2010 at 18:53 @Josh Stodola why one should not be using AJAX in this situation Commented Jun 29, 2017 at 15:22Just for information, the below
window.open("data:application/pdf," + encodeURI(pdfString));
does not work anymore in Chrome. Yesterday, I came across with the same issue and tried this solution, but did not work (it is 'Not allowed to navigate top frame to data URL'). You cannot open the data URL directly in a new window anymore. But, you can wrap it in iframe and make it open in a new window like below. =)
let pdfWindow = window.open("") pdfWindow.document.write( "" )
1,212 7 7 gold badges 21 21 silver badges 32 32 bronze badges
answered Sep 5, 2017 at 23:35
Noby Fujioka Noby Fujioka
1,804 1 1 gold badge 14 14 silver badges 16 16 bronze badges
Works in Chrome but once loaded it consumes CPU and GPU and the tab says it is still loading. Tried with iframe , embed and object .
Commented Oct 5, 2018 at 10:21 @SivaKiran setTimeout-0 solve my problem like this setTimeout(() => < pdfWindow.document.write( ); pdfWindow.document.title = $ ; >, 0); Commented Jul 3, 2019 at 10:40this work great, the document opened in new tab. But, when I click the download icon on the right corner, nothing happen. How to make it downloadable?
Commented Aug 12, 2019 at 6:53The tab still loading issue is very odd. 4 of our team members used the same button to open the pdf, and for 2 people it shows loading in the favicon, but for 2 of us it doesnt show the loading animation. everyone is using the same version of chrome.
Commented May 6, 2020 at 20:21 setTimeout(function()< pdfWindow.stop() >, 1000); at the end stops the loading issue Commented Apr 8, 2021 at 20:39var byteCharacters = atob(response.data); var byteNumbers = new Array(byteCharacters.length); for (var i = 0; i < byteCharacters.length; i++) < byteNumbers[i] = byteCharacters.charCodeAt(i); >var byteArray = new Uint8Array(byteNumbers); var file = new Blob([byteArray], < type: 'application/pdf;base64' >); var fileURL = URL.createObjectURL(file); window.open(fileURL);
You return a base64 string from the API or another source. You can also download it.
3,898 11 11 gold badges 32 32 silver badges 36 36 bronze badges answered Aug 30, 2018 at 7:50 Himanshu Chawla Himanshu Chawla 1,077 8 8 silver badges 8 8 bronze badgesThis seems like by far the best solution. Ever other solution seemed to work sporadically at best. I kept trying to encode the string responses from the server, but this seemed to be the only sure fire way. Thanks!
Commented Mar 23, 2019 at 3:10 I m getting error "failed to execute atob to window.any help Commented Jan 17, 2020 at 5:04Is there a way to obfuscate the url. I want to be the name of the file. Right now it shows as encoded base 64 value?
Commented Mar 6, 2020 at 20:22Wish I could upvote this a hundred times! I've been struggling to get larger files to open for days. Thank you ever so much!
Commented Sep 17, 2020 at 8:08@himanshu-chawla awseome answer! As a side question - is there a way to define the default file name when clicking download button, so it doesn't look like 6a5e4cd0-2db4-4c8c-80b8-15b77f911700.pdf but myFile.pdf ?
Commented Jul 7, 2021 at 8:09You might want to explore using the data URI. It would look something like.
window.open("data:application/pdf," + escape(pdfString));
I wasn't immediately able to get this to work, possible because formating of the binary string provided. I also usually use base64 encoded data when using the data URI. If you are able to pass the content from the backend encoded you can use..
window.open("data:application/pdf;base64, " + base64EncodedPDF);
Hopefully this is the right direction for what you need. Also note this will not work at all in IE6/7 because they do not support Data URIs.
answered May 10, 2010 at 19:56 Morgan ARR Allen Morgan ARR Allen 10.7k 3 3 gold badges 36 36 silver badges 33 33 bronze badgeswon't work in IE 8+ either, because the URL size will most likely be > 2048 character limit that IE imposes on URLs
Commented Sep 19, 2014 at 19:31I believe this restriction does not apply to Data URIs, only URLs. IE8 has a 32KB Data URI limit that was removed in IE9.
Commented Sep 20, 2014 at 20:02That's great but window.open(url) takes a URL, not a URI. IE chops off anything after 2048 chars, even if you use a data URI. Try this jsfiddle in IE, you'll see it doesn't work: jsfiddle.net/bpdj7ksv
Commented Sep 21, 2014 at 13:33This doesn't work for me window.open('data:application/pdf;base64,' + btoa(unescape(encodeURIComponent(resp))), "_blank", "toolbar=yes, scrollbars=yes, resizable=yes");
Commented Jul 8, 2015 at 14:21This one worked for me.
window.open("data:application/octet-stream;charset=utf-16le;base64,"+data64);
This one worked too
let a = document.createElement("a"); a.href = "data:application/octet-stream;base64,"+data64; a.download = "documentName.pdf" a.click();
answered Mar 8, 2018 at 4:05
Felipe Bejarano Felipe Bejarano
526 5 5 silver badges 8 8 bronze badges
I put the second part of code in a javascript function and called it onclick function of buttom and get answer very well.
Commented Jun 30, 2023 at 20:17window.open("data:application/pdf," + escape(pdfString));
The above one pasting the encoded content in URL. That makes restriction of the content length in URL and hence PDF file loading failed (because of incomplete content).
13.7k 13 13 gold badges 66 66 silver badges 80 80 bronze badges answered Jan 29, 2013 at 15:16 Ramprabhu Kaliaperumal Ramprabhu Kaliaperumal 229 2 2 silver badges 3 3 bronze badges warning: chrome detects this as pop-up and blocks Commented Nov 19, 2013 at 21:49 This will not work in MSIE due to the 2048 max character URL length MS decided to impose. Commented Sep 19, 2014 at 19:29This limit does not apply to Data URIs, only normal navigational URLs. Reference: msdn.microsoft.com/en-us/library/cc848897(v=vs.85).aspx
Commented Sep 20, 2014 at 20:10does anyone know why you wouldn't be able to interact with this window after opening it? like this var wdw = window.open("data:application/pdf," + escape(pdfString)); wdw.print(); The print function doesn't do anything. And I've even tried using a setTimeout to delay the print, but that doesn't work either. It's like it loses the reference to the window object. If I do the same thing but pass in an actual web url it works.
Commented Nov 8, 2016 at 13:58 how you solved such issue when the length of the content is too large ? Commented Apr 1, 2018 at 9:51I realize this is a pretty old question, but I had the same thing come up today and came up with the following solution:
doSomethingToRequestData().then(function(downloadedFile) < // create a download anchor tag var downloadLink = document.createElement('a'); downloadLink.target = '_blank'; downloadLink.download = 'name_to_give_saved_file.pdf'; // convert downloaded data to a Blob var blob = new Blob([downloadedFile.data], < type: 'application/pdf' >); // create an object URL from the Blob var URL = window.URL || window.webkitURL; var downloadUrl = URL.createObjectURL(blob); // set object URL as the anchor's href downloadLink.href = downloadUrl; // append the anchor to document body document.body.appendChild(downloadLink); // fire a click event on the anchor downloadLink.click(); // cleanup: remove element and revoke object URL document.body.removeChild(downloadLink); URL.revokeObjectURL(downloadUrl); >);