Skip to content

Commit

Permalink
fix not working export as pdf on linux, upgrade electron-packager to …
Browse files Browse the repository at this point in the history
…version 7.4.0
  • Loading branch information
dbackowski committed Aug 4, 2016
1 parent 84fc5aa commit 3ed6db9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
12 changes: 12 additions & 0 deletions app/pdf.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<style>
</style>
</head>
<body>
<script>
document.getElementsByTagName('style')[0].innerHTML = require('electron').remote.getGlobal('sharedObject').style;
document.getElementsByTagName('body')[0].innerHTML = require('electron').remote.getGlobal('sharedObject').body;
</script>
</body>
</html>
36 changes: 16 additions & 20 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const Menu = electron.Menu;
const dialog = electron.dialog;
const fs = require('fs');
const ipcMain = electron.ipcMain;
const convertFactory = require('electron-html-to');

let mainWindow;
let aboutWindow;
Expand Down Expand Up @@ -235,28 +234,25 @@ function exportAsPdfHandler() {
let pdfFileName = dialog.showSaveDialog({ filters: [{ name: 'pdf', extensions: ['pdf'] }]});

if (pdfFileName !== undefined) {
let conversion = convertFactory({
converterPath: convertFactory.converters.PDF
});
let pdfWindow = new BrowserWindow({ show: false });

let html_body = `<html>
<head>
<style>
${fs.readFileSync(__dirname + '/app/css/style.css').toString()}
</style>
</head>
<body>
${arg}
</body>
</html>`;
global.sharedObject = {
style: fs.readFileSync(__dirname + '/app/css/style.css').toString(),
body: arg
};

conversion({ html: html_body }, function(err, result) {
if (err) {
return dialog.showErrorBox('Unable to export as PDF', err.message);
}
pdfWindow.loadURL('file://' + __dirname + '/app/pdf.html');

pdfWindow.webContents.on("did-finish-load", function() {
pdfWindow.webContents.printToPDF({ printBackground: true }, function(error, data) {
if (error) throw error;

result.stream.pipe(fs.createWriteStream(pdfFileName));
conversion.kill();
fs.writeFile(pdfFileName, data, function(err) {
if (err) {
return dialog.showErrorBox('Unable to export as PDF', err.message);
}
})
})
});
}
});
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "MdEditor",
"version": "1.0.0",
"version": "1.0.1",
"description": "Simple markdown editor",
"main": "main.js",
"scripts": {
Expand All @@ -18,8 +18,7 @@
"devDependencies": {
"codemirror": "^5.14.2",
"css-loader": "^0.23.1",
"electron-html-to": "^2.0.2",
"electron-packager": "7.3.0",
"electron-packager": "7.4.0",
"electron-prebuilt": "1.3.1",
"extract-text-webpack-plugin": "^1.0.1",
"highlight.js": "^9.3.0",
Expand Down

0 comments on commit 3ed6db9

Please sign in to comment.