Print a pdf without visually opening it : <p>The thing I want to build is that by clicking a button I want to trigger the print of a PDF file, but without opening it.</p>

<pre><code>+-----------+
| Print PDF |
+-----------+
     ^ Click *---------&gt; printPdf(pdfUrl)
</code></pre>

<p>The way how I first tried it is to use an iframe:</p>

<pre><code>var $iframe = null;

// This is supposed to fix the onload bug on IE, but it's not fired
window.printIframeOnLoad = function() {
  if (!$iframe.attr("src")) { return; }
  var PDF = $iframe.get(0);
  PDF.focus();

  try {
    // This doesn't work on IE anyways
    PDF.contentWindow.print();

    // I think on IE we can do something like this:
    // PDF.document.execCommand("print", false, null);
  } catch (e) {
    // If we can't print it, we just open it in the current window
    window.location = url;
  }
};

function printPdf(url) {

  if ($iframe) {
    $iframe.remove();
  }

  $iframe = $('&lt;iframe&gt;', {
    class: "hide",
    id: "idPdf",
    // Supposed to be a fix for IE
    onload: "window.printIframeOnLoad()",
    src: url
  });

  $("body").prepend($iframe);
}
</code></pre>

<p>This works on Safari (desktop &amp; iOS) and Chrome (can we generalize it maybe to webkit?).</p>

<p>On Firefox, <code>PDF.contentWindow.print()</code> ends with a <code>permission denied</code> error (even the pdf is loaded from the same domain).</p>

<p>On IE (11), the <code>onload</code> handler is just not working.</p>

<hr>

<p>Now, my question is: is there another better way to print the pdf without visually opening it to the user?</p>

<p>The cross browser thing is critical here. We should support as many browsers as possible.</p>

<p>What's the best way to achieve this? Is my start a good one? How to complete it?</p>

<p><sub>We are now in 2016 and I feel like this is still a pain to implement across the browsers.</sub></p>
