代码之家  ›  专栏  ›  技术社区  ›  Always_a_learner

如何在新窗口中打开一个url,然后触发ctrl+p(打印)?

  •  0
  • Always_a_learner  · 技术社区  · 6 年前

    我有一个div中的iframe和一个print按钮,单击print我想打开一个新的选项卡并为iframe中显示的url触发print操作。 我用的是角7。

    到目前为止我尝试过的解决方案:

    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>
    

    组件.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();
    
          }
    

    解决方案来自 iframe inside angular2 component, Property 'contentWindow' does not exist on type 'HTMLElement'?

    使用上述解决方案时出错:

    错误域异常:阻止了具有原点的帧 “。” http://localhost:4200 “从访问交叉原点帧。

    我有一个网址,我只想在新的标签页中打开它,不需要点击ctrl+p就可以打印出来

    提前谢谢你的帮助!

    0 回复  |  直到 6 年前
        1
  •  1
  •   Abhijeet Chakravorty    6 年前

    See I am able to open the link in a new tab and the print window appears. —————--


    您可以安装这个npm模块并提供您希望执行的任何键组合。

    https://www.npmjs.com/package/angular2-hotkeys

    在新选项卡中打开URL时,必须提供一个目标空白的Href。

    你可以试试这个

      ngAfterViewInIt() {
               this.printView();
      }
    
    
       printView() { 
               window.print();
       }
    

    希望这有帮助:)。

    推荐文章