Skip to content

Commit

Permalink
add 添加 getPaperSizeInfo api
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier9896 committed Mar 9, 2024
1 parent 2f88c41 commit caad030
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,44 @@ socket.emit("refreshPrinterList");
// https://github.com/Xavier9896/node-hiprint-transit/blob/main/index.js#L139
```

## 获取打印机纸张信息

### 连接为 `electron-hiprint`

依赖 [win32-pdf-printer](https://www.npmjs.com/package/win32-pdf-printer) 包实现,暂时只支持 window 环境中使用!

> ❗️ node-hiprint-transit 中转暂未添加支持
```js
// printerName: 打印机名称 可选值,缺省时返回所有打印机的纸张信息
if (globalThis.connect) {
socket.emit("getPaperSizeInfo", printerName);
} else {
alert("未连接客户端!")
window.open("hiprint://")
}
```

```js
[
{
"PrinterName": "Microsoft Print to PDF",
"TaskNumber": 0, // 打印队列任务数
"Status": 0, // 打印机状态码
"StatusMsg": "准备就绪(Ready)", // 打印机状态信息
"PaperSizes": [
{
"Height": 1100,
"Kind": 1,
"PaperName": "信纸",
"RawKind": 1,
"Width": 850
},
{...}, {...}, {...}
]
}
]
```
## 打印 HTML

### 连接为 `electron-hiprint`
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"pdf-to-printer": "^5.6.0",
"socket.io": "^3.1.0",
"socket.io-client": "^3.1.0",
"unix-print": "^1.2.0"
"unix-print": "^1.2.0",
"win32-pdf-printer": "^4.0.1"
},
"devDependencies": {
"electron": "^15.0.0",
Expand Down
15 changes: 14 additions & 1 deletion tools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const address = require("address");
const ipp = require("ipp");
const { machineIdSync } = require("node-machine-id");
const Store = require("electron-store");
const { getPaperSizeInfo, getPaperSizeInfoAll } = require("win32-pdf-printer");
const log = require("./log");

Store.initRenderer();
Expand Down Expand Up @@ -176,7 +177,7 @@ function initServeEvent(server) {
* @description: client 请求客户端信息
*/
socket.on("getClientInfo", () => {
log(`插件端 ${socket.id} getClientInfo`);
log(`插件端 ${socket.id}: getClientInfo`);
emitClientInfo(socket);
});

Expand Down Expand Up @@ -216,6 +217,18 @@ function initServeEvent(server) {
socket.emit("printerList", MAIN_WINDOW.webContents.getPrinters());
});

/**
* @description: client 获取打印机纸张信息
*/
socket.on("getPaperSizeInfo", (printer) => {
log(`插件端 ${socket.id}: getPaperSizeInfo`);
if (process.platform === "win32") {
let fun = printer ? getPaperSizeInfo : getPaperSizeInfoAll;
let paper = fun();
paper && socket.emit("paperSizeInfo", paper);
}
});

/**
* @description: client 调用 ipp 打印 详见:https://www.npmjs.com/package/ipp
*/
Expand Down

0 comments on commit caad030

Please sign in to comment.