Angular 静默打印集成指南

准备工作

整体架构

Angular 服务示例

import { Injectable } from '@angular/core';
import webPrintPdf from 'web-print-pdf';

@Injectable({ providedIn: 'root' })
export class SilentPrintService {
  async printHtml(html: string) {
    return webPrintPdf.printHtml(
      html,
      { paperFormat: 'A4', printBackground: true },
      { paperFormat: 'A4', printerName: '默认打印机' }
    );
  }
}

组件模板绑定按钮

@Component({
  template: `
    <button (click)="onPrint()" [disabled]="loading">
      {{ loading ? '打印中…' : '静默打印' }}
    </button>
  `
})
export class ReportPrintComponent {
  loading = false;
  constructor(private print: SilentPrintService) {}
  async onPrint() {
    this.loading = true;
    try {
      await this.print.printHtml(document.getElementById('report')!.innerHTML);
    } finally {
      this.loading = false;
    }
  }
}

实践建议

表格打印常见坑

与本站专题分工

常见问题

各平台客户端部署

免费下载客户端 查看 npm 包 开发文档