Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit ba81b43

Browse files
authored
Merge pull request #11 from NativePHP/printer-support
Add printer API endpoints
2 parents 638fe4e + 0ec5e41 commit ba81b43

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

dist/server/api/system.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,26 @@ router.post('/prompt-touch-id', (req, res) => __awaiter(void 0, void 0, void 0,
3131
});
3232
}
3333
}));
34+
router.get('/printers', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
35+
const printers = yield electron_1.BrowserWindow.getAllWindows()[0].webContents.getPrintersAsync();
36+
res.json({
37+
printers,
38+
});
39+
}));
40+
router.post('/print', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
41+
const { printer, html } = req.body;
42+
let printWindow = new electron_1.BrowserWindow({
43+
show: false,
44+
});
45+
printWindow.webContents.on('did-finish-load', () => {
46+
printWindow.webContents.print({
47+
silent: true,
48+
deviceName: printer,
49+
}, (success, errorType) => {
50+
res.sendStatus(200);
51+
});
52+
printWindow = null;
53+
});
54+
yield printWindow.loadURL(`data:text/html;charset=UTF-8,${html}`);
55+
}));
3456
exports.default = router;

src/server/api/system.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import express from 'express'
2-
import {systemPreferences} from 'electron'
2+
import {BrowserWindow, systemPreferences} from 'electron'
33
const router = express.Router();
44

55
router.get('/can-prompt-touch-id', (req, res) => {
@@ -19,5 +19,32 @@ router.post('/prompt-touch-id', async (req, res) => {
1919
})
2020
}
2121
});
22+
router.get('/printers', async (req, res) => {
23+
const printers = await BrowserWindow.getAllWindows()[0].webContents.getPrintersAsync();
24+
25+
res.json({
26+
printers,
27+
});
28+
});
29+
30+
router.post('/print', async (req, res) => {
31+
const {printer, html} = req.body;
32+
33+
let printWindow = new BrowserWindow({
34+
show: false,
35+
});
36+
37+
printWindow.webContents.on('did-finish-load', () => {
38+
printWindow.webContents.print({
39+
silent: true,
40+
deviceName: printer,
41+
}, (success, errorType) => {
42+
res.sendStatus(200);
43+
});
44+
printWindow = null;
45+
});
46+
47+
await printWindow.loadURL(`data:text/html;charset=UTF-8,${html}`);
48+
});
2249

2350
export default router;

0 commit comments

Comments
 (0)