printer_service API 文档

运行在 armbian T95 主机(192.168.3.26)上的打印服务,通过本机 CUPS 驱动局域网打印机 (HP_SmartTank_580ipp://192.168.3.34/ipp/print)。

版本 1.0.0 · 文档路径 /docs · 规范 /openapi.json

接入信息

项目
公网地址https://19563.ai-x-service.top
本机地址http://127.0.0.1:19563
鉴权方式请求头 X-API-Key: <KEY>,或 Authorization: Bearer <KEY>,或 ?api_key=<KEY>
响应格式JSON(UTF-8)
密钥不在本页面展示。真实密钥保存在主机文件 /root/printer_service/config.json,并同步写入 /root/printer_service/docs/API.md 供交接使用。 查询命令:printer_service -show-key

快速开始

export BASE=https://19563.ai-x-service.top
export KEY=ps_xxxxxxxxxxxx   # 见 config.json / docs/API.md

# 1) 主机在线状态
curl -s -H "X-API-Key: $KEY" $BASE/api/host | jq .

# 2) 打印机状态
curl -s -H "X-API-Key: $KEY" $BASE/api/printers | jq .

# 3) 打印一个 PDF
curl -s -H "X-API-Key: $KEY" -F "file=@hello.pdf" $BASE/api/print | jq .

# 4) 打印一段文本
curl -s -H "X-API-Key: $KEY" -H "Content-Type: application/json" \
     -d '{"text":"Hello 打印机","job_name":"问候"}' $BASE/api/print | jq .

接口总览

方法路径说明参数
GET/health服务与主机存活探针(无需密钥)
GET/api/hostarmbian 主机在线状态:CPU 温度/负载、内存、磁盘、IP、CUPS 状态
GET/api/host/resources主机资源使用情况:CPU 使用率(总体/每核)/温度/频率、内存与交换、磁盘别名 /api/resources
GET/api/printers打印机列表:状态、是否接收任务、队列长度、耗材余量
GET/api/printers/{name}单台打印机详情 + 该打印机进行中的任务{name}=打印机名
GET/api/printers/{name}/raw打印机全部原始 IPP 属性(调试用){name}=打印机名
POST/api/print提交打印任务(multipart 文件 / JSON base64 / JSON 文本 / 原始字节流)见下方“打印参数”
POST/api/printers/{name}/test-page打印测试页(含主机状态),验证 armbian→CUPS→打印机 链路{name}=打印机名;可选 copies=N
GET/api/jobs任务列表which=active|completed|all, limit=N, printer=名称
GET/api/jobs/{id}单个任务状态{id}=任务 ID
POST/api/jobs/{id}/cancel取消任务{id}=任务 ID;可选 printer=名称
GET/api/whoami校验密钥是否有效
GET/docs本接口文档(HTML,无需密钥)
GET/openapi.jsonOpenAPI 3.0 规范(无需密钥)

打印参数(POST /api/print)

参数取值说明
printer打印机名缺省使用 CUPS 默认打印机
job_name字符串任务名,缺省取文件名
copies正整数份数
mediaiso_a4_210x297mm / na_letter_8.5x11in纸张尺寸
color_modecolor / monochrome(可写 彩色/黑白)色彩模式
sidesone-sided / two-sided-long-edge / two-sided-short-edge(可写 单面/双面)单双面
orientationportrait / landscape(可写 纵向/横向 或 3/4)方向
page_ranges1-3,5页码范围
resolution300x300300dpi打印分辨率(本机默认 600dpi,支持 300/600/1200)
document_formatMIME,如 application/pdf缺省按文件名后缀自动识别

支持 4 种请求体:
multipart/form-data,文件字段名 file
application/json + content_base64(可带 filename);
application/json + text(按 text/plain 打印);
④ 原始字节流(Content-Type: application/pdf 等)。

打印能力取决于 CUPS 过滤器,最稳妥的格式是 PDFPNG/JPEG纯文本。 打印机本身支持 PCL3GUI / PCLm / URF / PWG-Raster / JPEG,PDF 由 CUPS 自动转换。 建议先用 text/plain 或 PDF 验证链路,也可直接调用 /api/printers/{name}/test-page 打测试页。

输出字段说明

打印机状态 printer-state

idle 空闲 / processing 打印中 / stopped 停止(详见 state_reasons)。

任务状态 job-state

pending 排队 / processing 打印中 / completed 完成 / canceled 已取消 / aborted 失败。

错误返回

{"ok": false, "error": {"code": "cups_error", "message": "IPP 错误 ..."}}

调用示例

GET /health

服务与主机存活探针(无需密钥)

curl -s https://19563.ai-x-service.top/health

GET /api/host

armbian 主机在线状态:CPU 温度/负载、内存、磁盘、IP、CUPS 状态

curl -s -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/host

GET /api/host/resources

主机资源使用情况:CPU 使用率(总体/每核)/温度/频率、内存与交换、磁盘

curl -s -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/host/resources

GET /api/printers

打印机列表:状态、是否接收任务、队列长度、耗材余量

curl -s -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/printers

GET /api/printers/{name}

单台打印机详情 + 该打印机进行中的任务

curl -s -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/printers/HP_SmartTank_580

GET /api/printers/{name}/raw

打印机全部原始 IPP 属性(调试用)

curl -s -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/printers/HP_SmartTank_580/raw

POST /api/print

提交打印任务(multipart 文件 / JSON base64 / JSON 文本 / 原始字节流)

curl -s -H "X-API-Key: $KEY" -F "file=@a.pdf" https://19563.ai-x-service.top/api/print

POST /api/printers/{name}/test-page

打印测试页(含主机状态),验证 armbian→CUPS→打印机 链路

curl -s -X POST -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/printers/HP_SmartTank_580/test-page

GET /api/jobs

任务列表

curl -s -H "X-API-Key: $KEY" "https://19563.ai-x-service.top/api/jobs?which=active&limit=10"

GET /api/jobs/{id}

单个任务状态

curl -s -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/jobs/42

POST /api/jobs/{id}/cancel

取消任务

curl -s -X POST -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/jobs/42/cancel

GET /api/whoami

校验密钥是否有效

curl -s -H "X-API-Key: $KEY" https://19563.ai-x-service.top/api/whoami

GET /docs

本接口文档(HTML,无需密钥)

curl -s https://19563.ai-x-service.top/docs

GET /openapi.json

OpenAPI 3.0 规范(无需密钥)

curl -s https://19563.ai-x-service.top/openapi.json