forked from tojocky/node-printer
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathprintPDFInWindows.js
52 lines (41 loc) · 1.36 KB
/
printPDFInWindows.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
// Windows does not support PDF formats, but you can use imagemagick-native to achieve conversion from PDF to EMF.
var printer = require("../lib"),
imagemagick, // will be loaded later with proper error.
fs = require('fs'),
filename = process.argv[2],
printername = process.argv[2];
if(process.platform !== 'win32') {
throw 'This application can be run only on win32 as a demo of print PDF image'
}
if(!filename) {
throw 'PDF file name is missing. Please use the following params: <filename> [printername]'
}
try {
imagemagick = require('imagemagick-native');
} catch(e) {
throw 'please install imagemagick-native: `npm install imagemagick-native`'
}
var data = fs.readFileSync(filename);
console.log('data: ' + data.toString().substr(0, 20));
//console.log(imagemagick.identify({srcData: data}));
// First convert PDF into
imagemagick.convert({
srcData: data,
srcFormat: 'PDF',
format: 'EMF',
}, function(err, buffer) {
if (err) {
throw 'something went wrong on converting to EMF: ' + err;
}
// Now we have EMF file, send it to printer as EMF format
printer.printDirect({
data: buffer,
type: 'EMF',
success: function(id) {
console.log('printed with id ' + id);
},
error: function(err) {
console.error('error on printing: ' + err);
}
})
})