我想打印Angular 为2的HTML模板.我已经探索过了,我在Angular 1Print Html Template in Angularjs 1 中得到了解决方案

如有任何建议,我们将不胜感激.

推荐答案

这就是我在angular2中(类似于plunkered解决方案)在HTML文件中所做的:

<div id="print-section">
  // your html stuff that you want to print
</div>
<button (click)="print()">print</button>

在您的TS文件中:

print(): void {
    let printContents, popupWin;
    printContents = document.getElementById('print-section').innerHTML;
    popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto');
    popupWin.document.open();
    popupWin.document.write(`
      <html>
        <head>
          <title>Print tab</title>
          <style>
          //........Customized style.......
          </style>
        </head>
    <body onload="window.print();window.close()">${printContents}</body>
      </html>`
    );
    popupWin.document.close();
}

更新:

You can also shortcut the path and use merely 100 library for less inconsistent coding (mixing JS and TS) and more out-of-the-box controllable and secured printing cases.

Angular相关问答推荐

如何在HTML外部项目中使用Web组件(以17角创建)

使用当前选定的选项创建IF语句

命令不起作用的初始菜单项

如何包装NGB carousel ?

Angular 16:CSRF配置:我仍然可以使用HttpXsrfInterceptor和HttpXsrfCookieExtractor类吗?Inteli-J说他们不存在

Angular 17:在MouseOver事件上 Select 子组件的正确ElementRef

Angular 16 Auth Guard在具有0的服务时给出Null Injector错误;httpclient;被注入

无法在 app.component.html 中呈现自定义组件

JsPDF Autotable 将图像添加到列中出现错误:属性getElementsByTagName不存在

Angular 15 在 URL 中使用@进行路由

升级到 Webpack 5.79.0 后 NX Angular 项目构建失败

Angular6:RxJS switchMap 运算符未按预期工作

无法将项目从 Angular 13 更新到 14

带有数据的 Angular 模板

异步管道模板中的局部变量(Angular 2+)

Observable .do() 运算符 (rxjs) 的用例

缺少 .angular-cli.json 文件:Angular

由router-outlet 创建时如何将css类应用于组件元素?

Angular 2 更改事件 - 模型更改

为什么 Angular 项目中没有 Angular-CLI 为 model生成命令?