Skip to content

Instantly share code, notes, and snippets.

@rdtsc
Created March 17, 2015 03:48
Show Gist options
  • Save rdtsc/4a64c8c2b8183f6aee78 to your computer and use it in GitHub Desktop.
Save rdtsc/4a64c8c2b8183f6aee78 to your computer and use it in GitHub Desktop.
NW.js Windows PDF Printing Demo
body {
font-family: sans-serif;
}
section {
margin-bottom: 40px;
}
h1 {
font-size: 20px;
border-bottom: 1px solid #ccc;
}
#printer {
width: 270px;
}
document.addEventListener('DOMContentLoaded', function() {
'use strict';
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var wmic = exec('wmic Path Win32_Printer Get DeviceId',
function(error, stdout, stderr) {
if(error) throw error;
var printerList = document.getElementById('printer');
printerList.remove(0);
stdout.split('\n').splice(1).forEach(function(printerName) {
printerName = printerName.trim();
if(printerName) {
var entry = document.createElement('option');
entry.text = printerName;
printerList.add(entry);
}
});
});
document.getElementById('print-dialog').onclick = function() {
var pdf = document.getElementById('pdf').value.trim();
if(pdf) spawn('./SumatraPDF.exe', [
'-print-dialog', '-exit-when-done', pdf
]);
};
document.getElementById('print-silent').onclick = function() {
var pdf = document.getElementById('pdf').value.trim();
var printer = document.getElementById('printer');
if(pdf) spawn('./SumatraPDF.exe', [
'-print-to', printer[printer.selectedIndex].text, '-silent', pdf
]);
};
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NW.js Windows PDF Printing Demo</title>
<link rel="stylesheet" href="app.css">
<script src="app.js"></script>
</head>
<body>
<section>
<h1>0. Select a PDF</h1>
<input id="pdf" type="file" accept="application/pdf">
</section>
<section>
<h1>1. Classic Print Dialog</h1>
<button id="print-dialog">Print...</button>
</section>
<section>
<h1>2. Silent Printing</h1>
<select id="printer">
<option>Loading Printers...</option>
</select>
<button id="print-silent">Print</button>
</section>
</body>
</html>
{
"name": "nw.js-win-pdf-print-demo",
"main": "index.html",
"private": true,
"window": {
"width": 450,
"height": 360
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment