From 9b2a47136b34a4b9cfcc42282a39fdd909e9f8af Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Fri, 12 Apr 2019 17:45:57 -0400 Subject: enable print button --- electron/src/index.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'electron/src') diff --git a/electron/src/index.js b/electron/src/index.js index 398a9580..90aaea63 100644 --- a/electron/src/index.js +++ b/electron/src/index.js @@ -1,4 +1,5 @@ -import { app, BrowserWindow } from 'electron'; +import { app, BrowserWindow, ipcMain, dialog } from 'electron'; +var fs = require('fs'); // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -11,8 +12,16 @@ const createWindow = () => { mainWindow.maximize(); // and load the index.html of the app. - mainWindow.loadURL(process.argv[1]); + if (process.argv[1] == ".") { + // run in development mode with `electron . ` + var url = process.argv[2]; + } else { + var url = process.argv[1]; + } + mainWindow.loadURL(url); + mainWindow.webContents.openDevTools(); + // Emitted when the window is closed. mainWindow.on('closed', () => { // Dereference the window object, usually you would store windows @@ -31,3 +40,11 @@ app.on('ready', createWindow); app.on('window-all-closed', () => { app.quit(); }); + +ipcMain.on('print', function (event, pageSize) { + mainWindow.webContents.printToPDF({"pageSize": pageSize}, function(error, data) { + dialog.showSaveDialog(mainWindow, {"defaultPath": "inkstitch.pdf"}, function(filename, bookmark) { + fs.writeFileSync(filename, data, 'utf-8'); + }) + }) +}) \ No newline at end of file -- cgit v1.2.3