Reputation: 680
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);
}
Upvotes: 1
Views: 506
Reputation: 102
If you try this this?
var target= document.getElementById("myFrame");
try {
target.contentWindow.document.execCommand('print', false, null);
} catch(e) {
target.contentWindow.print();
}
Upvotes: 1