-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprint.js
More file actions
94 lines (81 loc) · 1.75 KB
/
print.js
File metadata and controls
94 lines (81 loc) · 1.75 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const {app, BrowserWindow, ipcMain} = require('electron');
const {dialog} = require('electron')
const fs = require('fs');
let print_win;
let save_pdf_path;
function getPDFPrintSettings() {
var option = {
landscape: false,
marginsType: 1,
printBackground: false,
printSelectionOnly: false,
pageSize: 'A4'
};
return option;
}
function print(event) {
if (print_win) {
print_win.webContents.print();
event.sender.send('printedInvoice', {
success: true
})
} else {
event.sender.send('printedInvoice', {
success: false
})
}
}
function printMainContent(event) {
if (print_win) {
print_win.webContents.print();
event.sender.send('printedMainContent', {
success: true
})
} else {
event.sender.send('printedMainContent', {
success: false
})
}
}
function savePDF(file_path) {
if (!print_win) {
dialog.showErrorBox('Error', "The printing window isn't created");
return;
}
if (file_path) {
print_win.webContents.printToPDF(getPDFPrintSettings(), function (err, data) {
if (err) {
dialog.showErrorBox('Error', err);
return;
}
fs.writeFile(file_path, data, function (err) {
if (err) {
dialog.showErrorBox('Error', err);
return;
}
save_pdf_path = file_path;
});
});
}
}
function viewPDF() {
if (!save_pdf_path) {
dialog.showErrorBox('Error', "You should save the pdf before viewing it");
return;
}
ipcMain.openItem(save_pdf_path);
}
function preparePrint(print) {
print_win = print;
print_win.show();
print_win.on('closed', function () {
print_win = null;
});
}
module.exports = exports = {
preparePrint,
savePDF,
viewPDF,
print,
printMainContent
};