npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

cainiao-printer

v1.0.1

Published

菜鸟打印组件 JavaScript SDK,支持通过 WebSocket 连接菜鸟打印组件进行打印

Readme

菜鸟打印组件 JavaScript SDK

一个用于连接菜鸟打印组件的 JavaScript 库,支持通过 WebSocket 与菜鸟打印组件通信,实现打印功能。

特性

  • ✅ 支持菜鸟打印组件 WebSocket 通信
  • ✅ 支持获取打印机列表
  • ✅ 支持使用自定义 XML 模板打印
  • ✅ 支持本地模板服务器
  • ✅ TypeScript 支持
  • ✅ 完整的错误处理和日志

安装

npm install
npm run build

快速开始

1. 确保菜鸟打印组件已安装并运行

菜鸟打印组件需要:

  • 安装菜鸟打印组件客户端
  • 组件服务正在运行(WebSocket 端口:13528/13529)

2. 基本使用

import { print, getPrinters } from './dist/index.esm.js';

// 获取打印机列表
const printers = await getPrinters();
console.log('可用打印机:', printers);

// 打印(使用模板)
await print({
  content: '', // HTML内容(可选,主要用于数据提取)
  cainiaoTemplateURL: 'http://localhost:8000/template/waybill.xml',
  cainiaoData: {
    expressCompany: '顺丰速运',
    trackingNumber: 'SF1234567890',
    sender: {
      name: '张三',
      phone: '13800138000',
      address: '北京市朝阳区测试街道123号'
    },
    receiver: {
      name: '李四',
      phone: '13900139000',
      address: '上海市浦东新区测试路456号'
    },
    remark: '易碎品,请轻拿轻放',
    printTime: new Date().toLocaleString('zh-CN')
  },
  paperSize: '75mm×130mm',
  onSuccess: () => {
    console.log('打印成功!');
  },
  onError: (error) => {
    console.error('打印失败:', error);
  }
});

模板服务器

项目包含一个本地模板服务器,用于提供 XML 模板文件:

npm run template-server

或者使用 Python HTTP 服务器:

cd template
python3 -m http.server 8000

模板格式

菜鸟打印组件使用 XML 格式的模板,参考 template/waybill.xmltemplate/waybill-simple.xml

详细说明请参考:

  • 菜鸟打印使用说明.md
  • 菜鸟打印自定义模板说明.md
  • 菜鸟打印本地模板说明.md
  • 模板格式说明.md

API 文档

print(options: PrintOptions): Promise<void>

执行打印任务。

参数:

  • options.content: HTML 内容(可选)
  • options.cainiaoTemplateURL: 模板 URL(必需)
  • options.cainiaoData: 模板数据(如果使用模板)
  • options.paperSize: 纸张大小,如 '75mm×130mm'
  • options.onSuccess: 成功回调
  • options.onError: 失败回调

getPrinters(): Promise<string[]>

获取可用打印机列表。

isSupported(): boolean

检测当前环境是否支持菜鸟打印。

示例

查看 examples/cainiao-example.html 获取完整示例。

文档

许可证

MIT