在线样例

请启用 JavaScript 后使用在线样例。

您的浏览器版本过旧,请升级至 Chrome、Edge、Firefox 或 Safari 最新版后再访问在线样例。

左侧浏览样例目录,右侧查看版式预览与示例代码;点击运行即可通过 web-print-pdf 驱动本地客户端完成静默打印或 PDF 预览。使用前请先 安装并启动客户端。

客户端

若打印或预览效果与预期不符,请务必先从官网下载并安装最新版 Web打印专家客户端后再试(旧版本可能导致版式偏差)。

版式说明

版式预览与打印输出完全遵循 HTML/CSS 标准样式,没有隐藏的渲染技巧:样式写成什么样,页面就长成什么样。

23 - 饮料价签

饮料价签打印样例。

示例代码

const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>饮料价签</title>
  <style>
    html, body { margin: 0; padding: 0; }
    .biz-sheet {
      position: relative;
      width: 227px;
      height: 151px;
      box-sizing: border-box;
      background: #fff;
      font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
      color: #0F172A;
      overflow: hidden;
    }
  </style>
</head>
<body>
  <div class="biz-sheet">
<div style="position:absolute;left:0px;top:0px;width:118px;height:151px;box-sizing:border-box;overflow:hidden;background:#075985;border:1px solid #075985;box-sizing:border-box"></div>
<div style="position:absolute;left:10px;top:10px;width:98px;height:14px;box-sizing:border-box;overflow:hidden;font-size:8px;color:#7DD3FC;display:flex;align-items:center;justify-content:flex-start;white-space:pre-wrap">悦享饮品</div>
<div style="position:absolute;left:128px;top:12px;width:60px;height:12px;box-sizing:border-box;overflow:hidden;font-size:6.5px;color:#0369A1;display:flex;align-items:center;justify-content:flex-start;white-space:pre-wrap">无糖气泡水</div>
<div style="position:absolute;left:128px;top:24px;width:91px;height:36px;box-sizing:border-box;overflow:hidden;font-size:12px;font-weight:700;color:#0F172A;white-space:pre-wrap">白桃味
气泡水</div>
<div style="position:absolute;left:34px;top:26px;width:74px;height:48px;box-sizing:border-box;overflow:hidden;font-size:32px;font-weight:700;color:#FFFFFF;text-align:right;display:flex;align-items:center;justify-content:flex-end;white-space:pre-wrap">5.50</div>
<div style="position:absolute;left:10px;top:32px;width:20px;height:24px;box-sizing:border-box;overflow:hidden;font-size:15px;color:#BAE6FD;display:flex;align-items:center;justify-content:flex-start;white-space:pre-wrap">¥</div>
<div style="position:absolute;left:128px;top:64px;width:91px;height:1px;box-sizing:border-box;overflow:hidden;background:#BAE6FD"></div>
<div style="position:absolute;left:128px;top:68px;width:30px;height:12px;box-sizing:border-box;overflow:hidden;font-size:6.5px;color:#64748B;display:flex;align-items:center;justify-content:flex-start;white-space:pre-wrap">规格</div>
<div style="position:absolute;left:10px;top:82px;width:98px;height:12px;box-sizing:border-box;overflow:hidden;font-size:8px;color:#BAE6FD;text-align:right;display:flex;align-items:center;justify-content:flex-end;white-space:pre-wrap">元 / 罐</div>
<div style="position:absolute;left:128px;top:82px;width:91px;height:14px;box-sizing:border-box;overflow:hidden;font-size:8px;color:#0F172A;display:flex;align-items:center;justify-content:flex-start;white-space:pre-wrap">330ml / 罐</div>
<div style="position:absolute;left:128px;top:98px;width:91px;height:36px;box-sizing:border-box;overflow:hidden;display:flex;flex-direction:column;align-items:center;justify-content:center"><svg width="91" height="22" viewBox="0 0 91 22" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none"><rect x="0" y="0" width="1" height="22" fill="#111"/><rect x="2" y="0" width="1" height="22" fill="#111"/><rect x="5" y="0" width="3" height="22" fill="#111"/><rect x="9" y="0" width="2" height="22" fill="#111"/><rect x="14" y="0" width="1" height="22" fill="#111"/><rect x="17" y="0" width="3" height="22" fill="#111"/><rect x="21" y="0" width="3" height="22" fill="#111"/><rect x="25" y="0" width="1" height="22" fill="#111"/><rect x="27" y="0" width="2" height="22" fill="#111"/><rect x="32" y="0" width="1" height="22" fill="#111"/><rect x="35" y="0" width="3" height="22" fill="#111"/><rect x="39" y="0" width="2" height="22" fill="#111"/><rect x="44" y="0" width="1" height="22" fill="#111"/><rect x="48" y="0" width="1" height="22" fill="#111"/><rect x="50" y="0" width="1" height="22" fill="#111"/><rect x="53" y="0" width="3" height="22" fill="#111"/><rect x="57" y="0" width="2" height="22" fill="#111"/><rect x="62" y="0" width="1" height="22" fill="#111"/><rect x="65" y="0" width="3" height="22" fill="#111"/><rect x="69" y="0" width="3" height="22" fill="#111"/><rect x="73" y="0" width="1" height="22" fill="#111"/><rect x="75" y="0" width="2" height="22" fill="#111"/><rect x="80" y="0" width="1" height="22" fill="#111"/><rect x="83" y="0" width="3" height="22" fill="#111"/><rect x="87" y="0" width="2" height="22" fill="#111"/></svg><div style="text-align:center;font-size:6px;line-height:1.2;margin-top:1px">6901234567892</div></div>
<div style="position:absolute;left:10px;top:102px;width:98px;height:12px;box-sizing:border-box;overflow:hidden;font-size:7px;color:#7DD3FC;text-align:right;display:flex;align-items:center;justify-content:flex-end;white-space:pre-wrap">建议零售价</div>
<div style="position:absolute;left:10px;top:124px;width:98px;height:12px;box-sizing:border-box;overflow:hidden;font-size:6px;color:#7DD3FC;text-align:right;display:flex;align-items:center;justify-content:flex-end;white-space:pre-wrap">冷藏 0-4℃ 饮用更佳</div>
<div style="position:absolute;left:128px;top:137px;width:91px;height:10px;box-sizing:border-box;overflow:hidden;font-size:5.5px;color:#94A3B8;display:flex;align-items:center;justify-content:flex-start;white-space:pre-wrap">生产日期:见罐身</div>
  </div>
</body>
</html>`;

await webPrintPdf.printHtml(html, {
  width: '60mm',
  height: '40mm',
  margin: { top: '0', bottom: '0', left: '0', right: '0' },
  printBackground: true
}, {
  // 实际打印时,须指定当前打印机上支持此尺寸的纸张名称(名称以 getPrinterPapers 返回为准),如果指定的纸张不符合此尺寸,效果是不对的。也可以不传递此参数,而是通过在当前软件打印机这个栏目的操作列,预先设置好 “打印首选纸张” 这个弹窗里的参数达到同样的效果
  paperFormat: '60x40',
  sharp: true /* 如果是热敏打印机,开启 sharp 抗锯齿,否则 false 即可 */
});

运行结果

  • 若无法打印或连接客户端失败,请参阅 本地Print succeeded,上线后无法打印?(HTTP 业务系统上线后常见)。
  • 客户端内置「运行示例」页(vueApp/.../examples/index.vue)提供更多可编辑 demo,与本页样例互补。
  • 工具类样例(连接检测、打印机列表等)仅返回 JSON,不触发打印。
免费下载客户端 查看 npm 包