8

Microsoft Edge is not handling window.print() consistently when compared with other browsers.

In most browsers calling window.print() from within an iframe on a page will only print the contents of that iframe. However in edge it will always print the whole document.

Was this intentional? Is there workaround?

Example on JSFiddle.

iframe.html

...
<body>
    <a href="#" onclick="window.print()">print iframe document</a>
</body>
...

index.html

...
<body>
    <a href="#" onclick="window.print()">print outer document</a>
    <iframe src="iframe.html"></iframe>
</body>
...
Stacked
  • 6,892
  • 7
  • 57
  • 73
Justin
  • 181
  • 1
  • 5

3 Answers3

6

This is a confirmed issue with microsoft, quote:

Posted by Microsoft on 7/29/2015 at 12:46 AM
We were able to confirm the issue, and will be working to resolve it in a future release

No workaround exists yet.

Dmitry Sadakov
  • 2,128
  • 3
  • 19
  • 34
4

We fount a magic solution:

parent.document.getElementsByName("pdfjs-frame")[0].contentWindow.document.execCommand("print", false, null);

...works in IE, EDGE, Chrome. The other was not tested yet.

prespic
  • 1,635
  • 1
  • 17
  • 20
  • Where do you run this code and how? and what happens if you have multiple PDF iframe's open? (when i just execute this code i get `Unable to get property 'contentWindow' of undefined or null reference`). – Peter Nov 04 '16 at 07:56
  • @tchelidze We have iframe with src and it it works, exacly this line of code. Just tested in IE and Chrome. @Peter Hi, sorry for late answer. First part of code is `parent.document.getElementsByName("pdfjs-frame")` It should find the i frame in whole document by name attribute. So in this example it will find – prespic Mar 27 '18 at 12:28
  • @prespic hmm, interesting, could you take a look at [this](https://stackoverflow.com/questions/49506664/unable-to-print-pdf-file-in-edge-browser) – tchelidze Mar 27 '18 at 12:31
  • @tchelidze It is about authorization, nothing common with this problem. I cant help you with this, sorry. – prespic Mar 27 '18 at 13:04
  • I am getting "Permission denied" error. This works in IE, but not in Edge. – Adrian Nov 09 '18 at 08:11
  • Just tested in EDGE, works fine. Simply put the line in console. Maybe it depends what iframe are you printing. We use it with [pdf.js](https://mozilla.github.io/pdf.js/) – prespic Nov 12 '18 at 08:57
-1

Its in IE too. You can fix it like this.

window.top.document.getElementById("iframe-id").contentWindow.focus();
window.top.document.getElementById("iframe-id").contentWindow.print();

It is tested. ;)