-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (60 loc) · 1.45 KB
/
index.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
68
69
70
71
72
73
const fs = require('fs')
const PDFDocument = require('pdfkit')
const ipp = require('ipp')
const PATH = './images'
const PRINTER = 'http://localhost:631/printers/Star_TSP143__STR_T_001_'
const PAGE_SIZE = [mm(80), mm(80)]
const PAPER_SIZE = 'Custom.80x80mm'
let files = []
fs.readdir(PATH, (err, f) => {
if (err) {
console.log(err)
process.exit()
}
files = f.filter(file => file != '.DS_Store')
console.log(`${files.length} images loaded.`)
printNext()
setInterval(printNext, 5500)
})
function printNext() {
const file = PATH + '/' + files[Math.floor(Math.random() * files.length)]
console.log(`Randomly selected: ${file}`)
printImage(file)
}
function printImage(f) {
const doc = new PDFDocument({autoFirstPage: false})
let buffer = []
doc.on('data', buffer.push.bind(buffer))
var page = doc.addPage({
size: PAGE_SIZE,
layout: 'landscape',
margin: 0
})
page.image(f, mm(25), mm(10), {
fit: [mm(30), mm(30)],
align: 'center',
valign: 'center'
})
doc.on('end', function() {
var file = {
'job-attributes-tag': {
'media': PAPER_SIZE
},
'operation-attributes-tag': {
'requesting-user-name': 'random',
'job-name': f,
'requesting-user-name': 'random',
'document-format': 'application/pdf',
},
data: Buffer.concat(buffer)
}
var printer = ipp.Printer(PRINTER)
printer.execute("Print-Job", file, function (err, res) {
delete buffer
})
})
doc.end()
}
function mm(mm) {
return mm * 2.834645669291
}