Reputation: 4189
In my angular 7 project I need to print some PDF and if possible I don't want to use a third part library so I found this code online:
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = blobUrl;
document.body.appendChild(iframe);
iframe.contentWindow.print()
It works perfect in chrome and opera but in firefox it prints a blank page. I try also with a setTimout when I print but I have an error in this case:
offsetParent is not set -- cannot scroll
ERROR DOMException: "Permission denied to access property "print" on cross-origin object"
So is there a solution for firefox?
p.s. without touching the browser preferences.
Upvotes: 1
Views: 1257
Reputation: 337
OK I found a solution:
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = blobUrl;
document.body.appendChild(iframe);
iframe.onload = () => {
setTimeout(() => {
iframe.focus();
iframe.contentWindow.print();
});
};
Upvotes: 4