-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap2pdf.js
More file actions
106 lines (89 loc) · 2.5 KB
/
Copy pathmap2pdf.js
File metadata and controls
106 lines (89 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const commandLineArgs = require('command-line-args')
const commandLineUsage = require('command-line-usage')
const optionDefinitions = [
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Display this usage guide.'
},
{
name: 'src',
type: String,
multiple: false,
defaultOption: true,
description: 'The input file to process',
typeLabel: '<file>'
}
]
const options = commandLineArgs(optionDefinitions)
const fs = require('fs')
const usage = commandLineUsage([
{
header: 'Hex Map SVG 2 Kingmaker PDF Converter',
content: 'Converts a svg hex map to a kingmaker map pdf.'
},
{
header: 'Options',
optionList: optionDefinitions
},
{
content: 'Project home: {underline https://github.com/me/example}'
}
])
if (options.help) {
console.log(usage);
process.exit();
} else {
const valid =
options.help ||
(
/* all supplied files should exist and --log-level should be one from the list */
options.src &&
fs.existsSync(options.src)
)
if (!valid) {
console.log(usage);
process.exit();
}
/*console.log('Your options are', valid ? 'valid' : 'invalid')
console.log(options)*/
}
var path = require('path'),
srcExt = path.extname(options.src),
srcFilename = path.basename(options.src, srcExt),
PDFDocument = require('pdfkit'),
SVGtoPDF = require('svg-to-pdfkit')
pdfFile = __dirname + '/' + srcFilename + '.pdf',
pdfFileTemp = __dirname + '/' + srcFilename + '_temp.pdf',
stream = fs.createWriteStream(pdfFileTemp)
;
const factor = 841.89 / 297;
var svg = fs.readFileSync(__dirname + '/' + srcFilename + '_rotated.svg', 'utf8');
//console.log("svg", svg);
// Create a document
var doc = new PDFDocument({
layout: 'portrait',
size: [212.7 * factor, 276.2 * factor] // a smaller document for small badge printers
});
doc.font(__dirname + '/fonts/Inkfree.ttf')
.fontSize(25)
.text('Greenbelt', 118 * factor, 19.5 * factor, {width: 69 * factor, align: 'center'});
SVGtoPDF(doc, svg, -1.411 * factor, 7 * factor, {width: 210 * factor});
// Stream contents to a file
stream.on('finish', function() {
console.log('PDF created');
const HummusRecipe = require('hummus-recipe');
const pdfDoc = new HummusRecipe(__dirname + '/map_kingmaker_template.pdf', pdfFile);
pdfDoc
.editPage(1)
.overlay(pdfFileTemp)
.endPage()
.endPDF(() => {
/* done! */
console.log("PDF overlayed");
});
});
// Close PDF and write file.
doc.pipe(stream);
doc.end();