Pdf.js: Printing from iframe in IE11 prints surrounding page instead of document

Created on 22 May 2019  ·  3Comments  ·  Source: mozilla/pdf.js

Note:
This was reported before in #7711 and probably in #6152, but incorrectly closed as fixed or not relevant

Configuration:

  • Web browser and its version: IE 11.0.9600.19356, Update Version 11.0.125
  • Operating system and its version: Windows 7
  • PDF.js version: 2.0.943
  • Is a browser extension: no

Steps to reproduce the problem:

  1. Load viewer into iframe
  2. Trigger a print using javascript from the main page (iframe.contentWindow.print())

Testcase
https://kleins05.github.io/pdf.js/test-ieiframe/
(load in IE11, click print button, check print output)

What is the expected behavior?
The PDF in the iframe should be printed.

What went wrong?
The surrounding page is printed.
Screen Shot 2019-05-22 at 13 21 01

Most helpful comment

I found the reason. Unlike other browsers window.print in IE causes the currently focused frame/window to print, not necessarily the one that window refers to. Using document.execCommand avoids this.

In web/pdf_print_service.js:187 instead of print.call(window); use:

var result = window.document.execCommand('print', false, null);
if (!result) {
   print.call(window);
}

If you are happy to accept a pull request in spite of your limited support of IE11, I am willing to create one. Let me know.

All 3 comments

Printing support in IE11 is limited in general, furthermore please refer to the relevant section of the FAQ:

While the library and the default viewer should still work in IE 11/Edge (non-Chromium-based) for the time being, some functionality/features may not be available and the performance will be worse compared to modern browsers. Going forward, only bugs which completely prevent the library and/or the default viewer from running will be accepted.

I found the reason. Unlike other browsers window.print in IE causes the currently focused frame/window to print, not necessarily the one that window refers to. Using document.execCommand avoids this.

In web/pdf_print_service.js:187 instead of print.call(window); use:

var result = window.document.execCommand('print', false, null);
if (!result) {
   print.call(window);
}

If you are happy to accept a pull request in spite of your limited support of IE11, I am willing to create one. Let me know.

If you are happy to accept a pull request in spite of your limited support of IE11,

Unfortunately we've never accepted compatibility "hacks" placed in the main code-base, for various reasons such as e.g. readability/maintainability/performance, and those were always restricted to polyfills placed in https://github.com/mozilla/pdf.js/blob/master/src/shared/compatibility.js

Furthermore, at this point in time a bug needs to prevent using the library at all in IE11 to be considered valid. (Expect a future PDF.js release, which one still to be decided as far as I know, to mark IE11 as explicitly unsupported and remove what little compatibility code that remains. Historically trying to support IE11 has restricted the ability to use modern JavaScript features, and has forced contributors to spend a lot of time supporting an essentially dead browser.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zerr0s picture zerr0s  ·  3Comments

aaronshaf picture aaronshaf  ·  3Comments

smit-modi picture smit-modi  ·  3Comments

liuzhen2008 picture liuzhen2008  ·  4Comments

PeterNerlich picture PeterNerlich  ·  3Comments