通过zebra通过zpl语言实现中文打印(一|二)这两篇文章其实已经知道了大概流程,对于数字和英文打印没有问题,接下来讲讲关于中文字体的问题。
1需要通过Zebra Setup Utilities这个软件进行下载安装,
加载字体后,我们在页面中添加一个按钮,点击打印字体列表。看是否正常加载进来了
const sendAndReadAllAvailable = (device: any, data: string, retries = 10): Promise<string> => { return new Promise((resolve, reject) => { if (typeof device?.sendThenReadUntilStringReceived !== 'function') { reject(new Error('BrowserPrint SDK method not available')) return } device.sendThenReadUntilStringReceived( data, '', (resp: string) => resolve(resp ?? ''), (err: any) => reject(err), retries ) }) } const getFontFileCandidates = async (device: any): Promise<string[]> => { const drives = ['R', 'E', 'B'] const seen = new Set<string>() const out: string[] = [] for (const drive of drives) { const query = `^XA^HW${drive}:*.*^XZ` const resp = await sendAndReadAllAvailable(device, query, 10).catch(() => '') const lines = String(resp || '') .split(/\r?\n/) .map((v) => v.trim()) .filter(Boolean) for (const line of lines) { const m = line.match(/([A-Z0-9_~\-]{1,32}\.(?:TTF|TTE|FNT))/i) const file = m?.[1]?.toUpperCase() if (!file) continue const spec = `${drive}:${file}` if (seen.has(spec)) continue seen.add(spec) out.push(spec) } } return out } const listFontFiles = async () => { printerStatus.value = '' try { const selected_device = await getPrinter() const candidates = await getFontFileCandidates(selected_device) const grouped: Record<string, string[]> = {} for (const spec of candidates) { const drive = spec.slice(0, 1) const file = spec.slice(2) if (!grouped[drive]) grouped[drive] = [] grouped[drive].push(file) } const drives = ['R', 'E', 'B'] const chunks: string[] = drives .filter((d) => grouped[d]?.length) .map((d) => `${d}: ${grouped[d].slice(0, 60).join(' | ')}`) const msg = chunks.length ? chunks.join('\n') : '未读取到字体文件(或打印机不支持 ^HW 回读)。' console.log('字体目录:\n' + msg) printerStatus.value = msg } catch (e: any) { console.error('读取字体目录失败:', e) printerStatus.value = '读取字体目录失败:' + (e?.message || String(e)) } }字体目录中有SIMSUN.FNT就是宋体。既然知道有宋体,我们在输出时在zpl代码里加入字体指令即可。还记得我们批量打印函数中fontFile参数
export const printOrderLabelBatch = async (options: { device: any items: Array<{ cardNo?: string; userId?: string | number; recognizedInfoString?: string }> xOffset?: number widthDots?: number heightDots?: number retries?: number retryDelayMs?: number timeoutMs?: number delayBetweenMs?: number fontFile?: string }) => { const device = options.device const items = Array.isArray(options.items) ? options.items : [] const delayBetweenMs = Math.max(0, Number(options.delayBetweenMs ?? 800)) const fontFile = normalizeZplFontFile(options.fontFile ?? 'SIMSUN.FNT') const zplList: string[] = [] for (const item of items) { const zpl = buildOrderLabelZpl({ cardNo: item?.cardNo, userId: item?.userId, recognizedInfoString: item?.recognizedInfoString, fontFile, xOffset: options.xOffset, widthDots: options.widthDots, heightDots: options.heightDots }) zplList.push(zpl) } for (let i = 0; i < zplList.length; i++) { await sendZplToPrinter(device, zplList[i], { retries: options.retries, retryDelayMs: options.retryDelayMs, timeoutMs: options.timeoutMs }) if (delayBetweenMs > 0 && i < zplList.length - 1) { await new Promise((r) => setTimeout(r, delayBetweenMs)) } } return zplList }默认我们采用了宋体:const fontFile = normalizeZplFontFile(options.fontFile ?? 'SIMSUN.FNT')