React silent printing guide

Use web-print-pdf in React 18 for silent printing—the local client renders HTML to PDF without a browser dialog. Same architecture as the Vue guide; below is a React Hook pattern.

Prerequisites

Architecture

React calls printHtml → npm submits via local WebSocket → client prints silently. See window.print alternative for the full picture.

React Hook example

Wrap useSilentPrint and pass print-area HTML on button click:

import { useCallback, useState } from 'react';
import webPrintPdf from 'web-print-pdf';

export function useSilentPrint() {
  const [loading, setLoading] = useState(false);

  const printHtml = useCallback(async (html) => {
    setLoading(true);
    try {
      return await webPrintPdf.printHtml(
        html,
        { paperFormat: 'A4', printBackground: true },
        { paperFormat: 'A4', printerName: '默认打印机' }
      );
    } finally {
      setLoading(false);
    }
  }, []);

  return { loading, printHtml };
}

Use in a component

import { useSilentPrint } from './useSilentPrint';

export function OrderPrintButton({ html }) {
  const { loading, printHtml } = useSilentPrint();
  return (
    <button disabled={loading} onClick={() => printHtml(html)}>
      {loading ? '打印中…' : '静默打印'}
    </button>
  );
}

Tips

  • Isolate print HTML/CSS; use @media print or a dedicated stylesheet.
  • Silent print avoids window.print dialogs—common in intranet policies.
  • Preflight with webPrintPdf.utils.getConnectStatus().
  • Works the same in Ant Design Pro, Umi, CRA, or Vite—import the npm package and call APIs.

FAQ

Does React silent print need a browser plugin?

No browser plugin—install the Web Print Expert client and npm install web-print-pdf.

Does it work with Next.js?

Yes—in client components ("use client"), import and call printHtml; printing runs in the browser only.

Difference from the Vue guide?

Same npm API; React uses Hooks/events. See the Vue page for WebSocket protocol details.

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