Skip to content

Instantly share code, notes, and snippets.

@jlord
Last active February 24, 2016 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlord/c883071b3123314e4487 to your computer and use it in GitHub Desktop.
Save jlord/c883071b3123314e4487 to your computer and use it in GitHub Desktop.
var printPDFBton = document.getElementById('print-pdf');
var ipc = require('electron').ipcRenderer;
printPDFBton.addEventListener('click', function (event) {
ipc.send('print-to-pdf');
});
ipc.on('wrote-pdf', function (event, path) {
// TODO should we open the pdf? or put it somewhere else?
var message = 'Wrote PDF to: ~' + path;
document.getElementById('pdf-path').innerHTML = message;
});
var fs = require('fs');
var BrowserWindow = require('browser-window');
var ipc = require('electron').ipcMain;
var shell = require('shell');
module.exports.setup = function () {
ipc.on('print-to-pdf', function (event) {
var pdfPath = '/tmp/print.pdf';
var win = BrowserWindow.fromWebContents(event.sender);
// Use default printing options
win.webContents.printToPDF({}, function (error, data) {
if (error) throw error;
fs.writeFile(pdfPath, data, function (error) {
if (error) {
throw error;
}
shell.openItem(pdfPath);
event.sender.send('wrote-pdf', pdfPath);
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment