1

I've written code that displays a pdf blob onto the screen of an iframe, but I also want it to print. I have this working in all browsers execept for IE11. Anyone know the solution there? I read some where about execCommand, but that didn't seem to work either.

const printElem = (invoice) => {
  const origiframe = document.querySelector('iframe');
  if (origiframe) {
    origiframe.remove();
  }
  const iframe = document.createElement('iframe');
  iframe.src = invoice;
  iframe.name = 'pdf-frame';
  iframe.id = 'pdf-frame';
  iframe.style.display = 'none';
  iframe.style.visibility = 'hidden';
  document.body.appendChild(iframe);
  window.frames['pdf-frame'].print();
}

UPDATE: I should be able to use something like the following to get the print to work across all browsers, but unclear on the syntax:

window.frames['pdf-frame'].document.execCommand('print',false,null);

UPDATE2: I'm trying to use the following as well, but still no dice. Anyone have any thoughts on why the catch portion won't work in IE11?

try {
  window.frames['pdf-frame'].print();
} catch(e) {
  window.frames['pdf-frame'].document.execCommand('print',false,null);
}
pingeyeg
  • 632
  • 2
  • 6
  • 23
  • What is the error in IE11? – Matthew Herbst Feb 06 '19 at 21:34
  • Object doesn't support property or method 'print' – pingeyeg Feb 06 '19 at 21:35
  • Duplicate of https://stackoverflow.com/questions/28774184/object-doesnt-support-property-print-ie-11. That question has a comment with a link to https://stackoverflow.com/questions/18888131/print-pdf-file-in-iframe-using-javascript-getting-one-page-only which may solve your question. – Matthew Herbst Feb 06 '19 at 21:37
  • @MatthewHerbst that does not help solve my issue. I'm using an iframe and everything works just fine in all browsers except for IE11. I would like to keep my setup, but append to it for IE11. – pingeyeg Feb 06 '19 at 21:43

1 Answers1

1

If you try this this?

var target= document.getElementById("myFrame");
try {
    target.contentWindow.document.execCommand('print', false, null);
} catch(e) {
    target.contentWindow.print();
}
tahtoh
  • 102
  • 8
  • This brings in an error of 'Unable to get property 'print' of undefined or null reference', yet the iframe is on the screen with the correct id. – pingeyeg Feb 06 '19 at 21:48
  • Interestingly enough, when I use your code to target the iframe for print, it no longer outputs the data. Only my code prints out the data. – pingeyeg Feb 06 '19 at 21:52
  • if you test this on your browser ie 11 it doesnt work? https://codepen.io/anon/pen/QYOvJG im not in front of computer atm – tahtoh Feb 06 '19 at 22:18
  • Unfortunately, CodePen doesn't work in IE11. I tried firing up your pen, but CodePen said it would only work in a different mode, which I would have to pay for. – pingeyeg Feb 07 '19 at 12:35
  • So, based on what I saw on another post, it looks like I should be able to use window.frames['pdf-frame'] to grab the contentWindow, but now I'm unsure how to make this statement work: window.frames['pdf-frame'].document.execCommand('print',false,null); – pingeyeg Feb 07 '19 at 12:48
  • I went ahead and approved your answer as it is the best answer possible for most browsers, but I actually wound up using a library called printJS that works quite well for the browsers that actually allow printing from an iframe. The other browsers I'm just opening the pdf in another tab as a default. – pingeyeg Feb 14 '19 at 18:13