I have iframe within a div and have a print button, on clicking print I want to open a new tab and trigger print action for the url being shown in iframe. I am using Angular 7.
Solutions I tried so far:
How to open a link new tab with print command?
html
<div>
<iframe #iframe [src]="url" width="100%" id='event-print-iframe'
name="targetframe" allowTransparency="true" frameborder="0" >
</iframe>
<button (click)='print()'>Print</button>
</div>
component.ts
import {
//...
ElementRef,
ViewChild
} from '@angular/core';
@ViewChild('iframe') iframe: ElementRef;
print()
{
let content = this.url;
let doc = this.iframe.nativeElement.contentDocument || this.iframe.nativeElement.contentWindow;
doc.open();
doc.write(content);
doc.close();
}
Solution From iframe inside angular2 component, Property 'contentWindow' does not exist on type 'HTMLElement'?
Error using above solution:
ERROR DOMException: Blocked a frame with origin "http://localhost:4200" from accessing a cross-origin frame.
I have a url and I just want to open it in new tab and print without clicking Ctrl+p
Thanks in advance for helping!