-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipp.js
67 lines (53 loc) · 2.32 KB
/
ipp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const fs = require('fs')
const util = require('util');
const nonPrivate = require('non-private-ip')
const logger = require('./logger');
const url = require('url')
const Printer = require('ipp-printer');
const catPrinter = require('./printer');
const image = require('./image');
const childProcess = require('child_process');
const cli = require('./cli');
const exec = util.promisify(childProcess.exec);
const args = cli.args;
const ip = nonPrivate() || nonPrivate.private()
const config = { name: 'Cat Printer uwu', dir: process.cwd(), port: 3000}
const printer = new Printer(config)
module.exports = {
/**
* Listens for print and handles jobs over IPP.
* @return {void}
*/
listenForPrintJobs: async function() {
logger.info('Starting in IPP / PS Mode...');
printer.server.on('listening', () => {
logger.info(`Cat printer listening on: ${url.format({protocol: 'http', hostname: ip, port: config.port})}`);
})
printer.on('job', async (job) => {
logger.info(`Print job received! : Id: ${job.Iid} Name:${job.name}`);
const filename = 'job-' + job.id + '.ps' // .ps = PostScript
const file = fs.createWriteStream(filename)
job.pipe(file);
job.on('end', async () => {
logger.info(`Print job saved as: ${filename}`);
logger.trace('Running ghostscript to convert to PNG...');
//step 1, PS -> PNG with ghostscript
//TODO: find a way to make this less shit!
await exec(
`gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngmono -sOutputFile=${filename}.png ${filename}`
);
//Step 2, convert to format for printer
logger.trace(`Connecting to cat printer ${args.devicename}...`);
await catPrinter.connect(args.devicename, args.timeout);
//Step 3, print as normal
const toPrint = await image.process(`${filename}.png`, true);
await catPrinter.print(toPrint, 'image', true);
//Step 4, delete temp files
logger.trace('Cleaning up temp files...');
await exec(
`rm ${filename} && rm ${filename}.png`
)
})
})
}
}