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: 'Default printer' }
);
}
// #endif
<template>
<view>
<view id="print-area" v-html="reportHtml"></view>
<button @click="onPrint">Silent print</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.uni.print, web-print-pdf supports silent named-printer output.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 enqueue jobs on your server; desk clients connect to your exposed API—not printHtml inside the mini program. 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.