Silent print in mini programs?
Not to PC clients—use H5 desks or remote push.
When UniApp builds to H5 in a desktop browser, integrate web-print-pdf like any web app. Mini programs cannot drive PC printers directly—use H5 stations or remote push to PCs.
web-print-pdf via npm or static import—same API as VueUniApp H5 calls printHtml → local WebSocket client → printer. Same as the Vue guide.
Import and wrap in setup or Options API:
// #ifdef H5
import webPrintPdf from 'web-print-pdf';
export async function silentPrint(html) {
return webPrintPdf.printHtml(
html,
{ paperFormat: 'A4', printBackground: true },
{ printerName: '默认打印机' }
);
}
// #endif
<template>
<view>
<view id="print-area" v-html="reportHtml"></view>
<button @click="onPrint">静默打印</button>
</view>
</template>
<script setup>
// #ifdef H5
import { silentPrint } from '@/utils/print';
const onPrint = () => silentPrint(document.getElementById('print-area').innerHTML);
// #endif
</script>
WeChat/Alipay mini programs cannot reach the desktop client—use server push to a PC station. See remote print.
#ifdef H5 around imports to avoid mini program bundle errors.ref or HTML strings like the Vue guide.Not to PC clients—use H5 desks or remote push.
Same printHtml API—mind UniApp conditional compile and DOM access.
Evaluate WebSocket to local client; H5 desks or remote print are more common in production.
WebView must reach 127.0.0.1 WebSocket—H5 desks or remote push are more common in production.
Mini programs push jobs to the server; warehouse/front PCs run H5 + client printHtml—see WMS and remote print guides.
The same web-print-pdf front-end runs on Windows, macOS, and Linux/domestic OS—install the matching client on each desktop.