-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
132 lines (111 loc) · 3.03 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
const {app, ipcMain, BrowserWindow} = require("electron");
const path = require("path");
let mainWindow;
let dev = false;
if (
process.defaultApp ||
/[\\/]electron-prebuilt[\\/]/.test(process.execPath) ||
/[\\/]electron[\\/]/.test(process.execPath)
) {
dev = true;
}
async function createWindow() {
mainWindow = new BrowserWindow({
width: 1024,
height: 768,
show: false,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
},
});
// load the index.html of the app
let indexPath;
if (dev && process.argv.indexOf("--noDevServer") === -1) {
indexPath = new URL("http://localhost:4000/index.html");
} else {
indexPath = new URL(path.join(__dirname, "dist", "index.html"), "file:");
}
mainWindow.once("ready-to-show", () => {
mainWindow.show();
if (dev) {
mainWindow.webContents.openDevTools();
}
});
mainWindow.on("closed", function () {
mainWindow = null;
// terminate the app when main window is closed
if (process.platform !== "darwin") {
app.quit();
}
});
await mainWindow.loadURL(indexPath.toString());
}
app.on("ready", createWindow);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", async () => {
if (mainWindow === null) {
await createWindow();
}
});
//-------------------- print function -----------------
// List of all options at -
// https://www.electronjs.org/docs/latest/api/web-contents#contentsprintoptions-callback
const printOptions = {
silent: false,
printBackground: true,
color: true,
margin: {
marginType: "printableArea",
},
landscape: false,
pagesPerSheet: 1,
collate: false,
copies: 1,
header: "Page header",
footer: "Page footer",
};
//handle print
ipcMain.handle("printComponent", async (event, url) => {
const win = new BrowserWindow({show: false});
win.webContents.on("did-finish-load", () => {
win.webContents.print(printOptions, (success, failureReason) => {
console.log("Print Initiated in Main...");
if (!success) console.log(failureReason);
});
});
await win.loadURL(url);
return "shown print dialog";
});
//handle preview
ipcMain.handle("previewComponent", async (event, url) => {
let win = new BrowserWindow({
title: "Print Preview",
show: false,
autoHideMenuBar: true
});
win.webContents.once("did-finish-load", () => {
win.webContents.printToPDF(printOptions).then((data) => {
const buf = Buffer.from(data);
data = buf.toString("base64");
const url = "data:application/pdf;base64," + data;
win.webContents.on("ready-to-show", () => {
win.once("page-title-updated", (e) => e.preventDefault());
win.show();
});
win.webContents.on("closed", () => win = null);
win.loadURL(url);
}).catch((error) => {
console.log(error);
});
});
await win.loadURL(url);
return "shown preview window";
});