Angular silent printing guide

In Angular 15+ admin apps, wrap web-print-pdf printHtml in an Injectable service for silent output. Same npm API as the Vue and React guides.

Prerequisites

Architecture

Component → Angular service → printHtml → local WebSocket client. See window.print alternative.

Angular service example

Create an injectable print service and call from components:

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: '默认打印机' }
    );
  }
}

Bind in template

@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;
    }
  }
}

Tips

  • No special angular.json config—run print logic in the browser only, not during SSR.
  • Angular Material tables: use @ViewChild on the table wrapper or a print-only template.
  • Preflight with getConnectStatus(); see error handling.
  • Works with NgRx/RxJS—call printHtml from click handlers or <code>firstValueFrom</code>.

Common Angular Material table print pitfalls

Printing MatTable often pulls paginator, sort headers, or sidenav chrome onto paper.

  • Sidenav/toolbar: clone table DOM only—not the whole mat-sidenav-container
  • Pagination: current page only by default—merge HTML or export server-side for full data
  • SSR: call printHtml from browser click handlers only—not during Universal server render
  • Fixed columns: may duplicate DOM like React/Ant Design tables

How this page fits with related guides

FAQ

Browser plugin required?

No—install the desktop client and npm package.

Angular SSR?

Call printHtml from browser click handlers only—not during server render.

vs React guide?

Same npm API—Angular uses Services/DI.

MatTable fixed columns print twice?

Remove fixed/sticky containers or use a print-only HTML template without Material chrome.

Print from NgRx async flows?

Call printHtml from component click handlers after effects complete—not inside reducers.

Client deployment by platform

The same web-print-pdf front-end runs on Windows, macOS, and Linux/domestic OS—install the matching client on each desktop.

Download client — free trial View npm package Documentation