news 2026/3/9 12:47:53

zebra通过zpl语言实现中文打印(三)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
zebra通过zpl语言实现中文打印(三)

通过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')

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/4 5:02:55

从回调函数到Promise

最近在面试中遇到了很多关于 Promise 的问题&#xff0c;因为以前的业务在请求方面并不复杂&#xff0c;多数时候都是在用 async/await&#xff0c;对 Promise 的理解还是有所欠缺&#xff0c;最近重新学习了一下 Promise&#xff0c;尽量避免写成API式的文章&#xff0c;主要还…

作者头像 李华
网站建设 2026/3/8 5:39:15

java+vue基于springboot农产品溯源系统_AI问答 农产品销售网上商城系统 w01c2pa0-Pycharm vue django项目源码

目录农产品溯源系统概述技术架构核心功能模块项目源码特点扩展应用部署与二次开发开发技术源码文档获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;农产品溯源系统概述 基于SpringBoot和Vue的农产品溯源系统整合了生产、流通、销售全流程数据&a…

作者头像 李华
网站建设 2026/3/7 15:13:35

java+vue基于springboot宠物店活动报名系统 宠物领养管理系统_nz2f939x

目录系统概述核心功能模块技术实现要点数据库设计示例部署与扩展开发技术源码文档获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;系统概述 基于SpringBoot和Vue的宠物店活动报名与领养管理系统&#xff0c;整合了宠物店日常运营中的活动管理与…

作者头像 李华
网站建设 2026/3/4 4:07:47

SG-CANFiber-120 (S)特点与功能介绍

一、核心定位与工作模式SG-CANFiber-120 (S) 是工业级单纤型 CAN 总线转光纤传输设备&#xff0c;核心功能是实现 CAN 总线电信号与光纤光信号的双向透明转换&#xff0c;支持环网、手拉手、点对点、星型四种组网方式&#xff0c;可自动识别连接类型无需手动配置。设备能将 CAN…

作者头像 李华