Trigger print preview of base64 encoded PDF from javascript
<p>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:</p>

<ol>
<li>Using <code>embed</code> element in a new window.  Calling <code>window.print()</code> is blank, even after the content is loaded.</li>
<li>Using a hidden, dynamically created <code>iframe</code> with <code>src="data:application/pdf;base64,JVBERi0..."</code> and calling <code>myFrame.contentWindow.print()</code>.  But this gives a CORS error.  I'm not sure why, because I'm not loading a new domain through the iframe, just content.</li>
<li>Open a new window with only an <code>iframe</code> element like the one in #2 and calling a print on the whole window.  This also shows a blank white page.</li>
<li>Open a new window with the data uri and print it.  <code>window.open('data:application/pdf;base64,JVBERi0...').print();</code>.  This doesn't work either, as it doesn't even show a print preview at all.  I've also tried delaying it with a <code>setTimeout</code> but that doesn't do anything either.</li>
</ol>

<p>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:</p>

<p><a href="https://i.stack.imgur.com/JwG12.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JwG12.png" alt="Chrome Print Preview"></a></p>

<p>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.</p>