dotfiles/config/scripts/html2pdf

57 lines
1.2 KiB
Plaintext
Raw Permalink Normal View History

2017-06-14 08:06:40 +02:00
#!/usr/bin/env node
// Imports
var fs = require('fs');
var pdf = require('html-pdf');
var yargs = require('yargs');
// Understanding
var argv = yargs
.usage("Usage: $0 -o out.pdf [options]")
.example('$0 -i doc.pdf -o doc.pdf', 'Convert doc.html to PDF using the default values')
.help('h')
.alias('h', 'help')
.describe('i', 'Input file')
.alias('i', 'input')
.default('i', '/dev/stdin')
.describe('o', 'Output file')
.alias('o', 'output')
.describe('t', 'Title of file')
.alias('t', 'title')
.default('t', 'Sans titre')
2017-09-17 18:42:53 +02:00
.describe('b', 'Border')
.alias('b', 'border')
.default('b', '2cm')
2017-06-14 08:06:40 +02:00
.demandOption(['o'])
.argv;
// Settings
options = {
"base": "file://" + process.cwd() + '/',
"format": "A4",
"orientation": "portrait",
2017-09-17 18:42:53 +02:00
"border": argv.border,
2017-06-14 08:06:40 +02:00
"footer": {
"height": "10mm",
"contents": {
default: '<div style="text-align: left; float: left;">' + argv.title + '</div> <div style="text-align:right; float: right;">{{page}}/{{pages}}</div>',
}
},
}
// Reading
htmlString = fs.readFileSync(argv.i, "utf8");
// Conversion
pdf.create(htmlString, options).toFile(argv.o, function(err, res) {
if (err) console.error(err);
});