I've looked around stackoverflow trying to find a way to do this for a while now, and can't find a suitable answer. I need to be able to load a PDF in either a new window or an iframe via a base64 encoded string and trigger a print preview of it immediately after loading it. I can easily load the PDF using both of those methods, but can't actually get it to show the print preview properly. Here is what I've tried:
- Using
embed
element in a new window. Callingwindow.print()
is blank, even after the content is loaded. - Using a hidden, dynamically created
iframe
withsrc="data:application/pdf;base64,JVBERi0..."
and callingmyFrame.contentWindow.print()
. But this gives a CORS error. I'm not sure why, because I'm not loading a new domain through the iframe, just content. - Open a new window with only an
iframe
element like the one in #2 and calling a print on the whole window. This also shows a blank white page. - Open a new window with the data uri and print it.
window.open('data:application/pdf;base64,JVBERi0...').print();
. This doesn't work either, as it doesn't even show a print preview at all. I've also tried delaying it with asetTimeout
but that doesn't do anything either.
At this point I'm very confused as to why none of these work, especially because in Chrome it display's custom menu bars like this:
And if I click the actual print icon there, the print preview is perfect. Whatever Chrome is doing when I click that button is exactly what I want to accomplish. Is there anyway to trigger that functionality? Or is there another way to accomplish what I want? And just to clarify, I only need this to work in Chrome, I don't need to worry about other browsers.