-
Notifications
You must be signed in to change notification settings - Fork 177
/
decktape.js
executable file
·604 lines (565 loc) · 20.6 KB
/
decktape.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#!/usr/bin/env node
'use strict';
import chalk from 'chalk';
import crypto from 'crypto';
import { Font } from 'fonteditor-core';
import fs from 'fs';
import os from 'os';
import parser from './libs/nomnom.js';
import path from 'path';
import puppeteer from 'puppeteer';
import URI from 'urijs';
import util from 'util';
import { fileURLToPath } from 'url';
import { PDFDocument, PDFName, ParseSpeeds, decodePDFRawStream } from 'pdf-lib';
import { delay, pause } from './libs/util.js';
parser.script('decktape').options({
url : {
position : 1,
required : true,
transform : parseUrl,
help : 'URL of the slides deck',
},
filename : {
position : 2,
required : true,
help : 'Filename of the output PDF file',
},
size : {
abbr : 's',
metavar : '<size>',
type : 'string',
callback : parseSize,
transform : parseSize,
help : 'Size of the slides deck viewport: <width>x<height> (e.g. \'1280x720\')',
},
pause : {
abbr : 'p',
metavar : '<ms>',
default : 1000,
help : 'Duration in milliseconds before each slide is exported',
},
loadPause : {
full : 'load-pause',
metavar : '<ms>',
default : 0,
help : 'Duration in milliseconds between the page has loaded and starting to export slides',
},
urlLoadTimeout : {
full : 'url-load-timeout',
metavar : '<ms>',
default : 60000,
help : 'Timeout in milliseconds to use when waiting for the initial URL to load',
},
pageLoadTimeout : {
full : 'page-load-timeout',
metavar : '<ms>',
default : 20000,
help : 'Timeout in milliseconds to use when waiting for the slide deck page to load',
},
bufferTimeout : {
full : 'buffer-timeout',
metavar : '<ms>',
default : 30000,
help : 'Timeout in milliseconds to use when waiting for a slide to finish buffering (set to 0 to disable)',
},
screenshots : {
default : false,
flag : true,
help : 'Capture each slide as an image',
},
screenshotDirectory : {
full : 'screenshots-directory',
metavar : '<dir>',
default : 'screenshots',
help : 'Screenshots output directory',
},
screenshotSizes : {
full : 'screenshots-size',
metavar : '<size>',
type : 'string',
list : true,
callback : parseSize,
transform : parseSize,
help : 'Screenshots resolution, can be repeated',
},
screenshotFormat : {
full : 'screenshots-format',
metavar : '<format>',
default : 'png',
choices : ['jpg', 'png'],
help : 'Screenshots image format, one of [jpg, png]',
},
slides : {
metavar : '<range>',
type : 'string',
callback : parseRange,
transform : parseRange,
help : 'Range of slides to be exported, a combination of slide indexes and ranges (e.g. \'1-3,5,8\')',
},
headless : {
default : 'new', // false to enable headed mode and true to enable old puppeteer headless. See: https://developer.chrome.com/articles/new-headless/#new-headless-in-puppeteer
help : 'Puppeteer headless mode, one if [new, true, false]',
},
headers : {
type : 'string',
callback : parseHeaders,
transform : parseHeaders,
help : 'HTTP headers, comma-separated list of <header>,<value> pairs (e.g. "Authorization,\'Bearer ASDJASLKJALKSJDL\'")',
},
// Chrome options
chromePath : {
full : 'chrome-path',
metavar : '<path>',
type : 'string',
help : 'Path to the Chromium or Chrome executable to run instead of the bundled Chromium',
},
chromeArgs : {
full : 'chrome-arg',
metavar : '<arg>',
type : 'string',
list : true,
help : 'Additional argument to pass to the Chrome instance, can be repeated',
},
// PDF meta data
metaAuthor : {
full : 'pdf-author',
metavar : '<arg>',
type : 'string',
help : 'String to set as the author of the resulting PDF document',
},
metaTitle : {
full : 'pdf-title',
metavar : '<arg>',
type : 'string',
help : 'String to set as the title of the resulting PDF document',
},
metaSubject : {
full : 'pdf-subject',
metavar : '<arg>',
type : 'string',
help : 'String to set as the subject of the resulting PDF document',
},
});
function parseHeaders(headerString) {
const h = headerString.split(",");
if ((h.length % 2) != 0) {
return 'header flag must be a comma delimited key value pairing and should always have an even number of kv pairs';
}
let headers = {};
for (let i = 0; i < h.length; i += 2) {
headers[h[i]] = h[i+1];
}
return headers;
}
function parseSize(size) {
// we may want to support height and width labeled with units
// /^(\d+(?:px)?|\d+(?:\.\d+)?(?:in|cm|mm)?)\s?x\s?(\d+(?:px)?|\d+(?:\.\d+)?(?:in|cm|mm)?)$/
const match = size.match(/^(\d+)x(\d+)$/);
if (match) {
const [, width, height] = match;
return { width: parseInt(width, 10), height: parseInt(height, 10) };
} else {
return '<size> must follow the <width>x<height> notation, e.g., \'1280x720\'';
}
}
function parseRange(range) {
const regex = /(\d+)(?:-(\d+))?/g;
if (!range.match(regex))
return '<range> must be a combination of slide indexes and ranges, e.g., \'1-3,5,8\'';
let slide, slides = {};
while ((slide = regex.exec(range)) !== null) {
const [, m, n] = slide.map(i => parseInt(i));
if (isNaN(n)) {
slides[m] = true;
} else {
for (let i = m; i <= n; i++) {
slides[i] = true;
}
}
}
return slides;
}
function parseUrl(url) {
const uri = URI(url);
if (!uri.protocol()) {
if (path.isAbsolute(url)) {
return 'file://' + path.normalize(url);
} else {
return 'file://' + path.normalize(path.join(process.cwd(), url));
}
}
return url;
}
parser.command('version')
.root(true)
.help('Display decktape package version')
.callback(_ => {
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url)));
console.log(pkg.version);
process.exit();
});
parser.nocommand()
.help(
`Defaults to the automatic command.
Iterates over the available plugins, picks the compatible one for presentation at the
specified <url> and uses it to export and write the PDF into the specified <filename>.`
);
parser.command('automatic')
.help(
`Iterates over the available plugins, picks the compatible one for presentation at the
specified <url> and uses it to export and write the PDF into the specified <filename>.`
);
// TODO: should be deactivated as well when it does not execute in a TTY context
if (os.name === 'windows') parser.nocolors();
const color = type => {
switch (type) {
case 'error': return chalk.red;
case 'warn': return chalk.yellow;
default: return chalk.gray;
}
};
process.on('unhandledRejection', error => {
console.log(error.stack);
process.exit(1);
});
(async () => {
const plugins = await loadAvailablePlugins(path.join(path.dirname(fileURLToPath(import.meta.url)), 'plugins'));
Object.entries(plugins).forEach(([id, plugin]) => {
const command = parser.command(id);
if (typeof plugin.options === 'object') {
command.options(plugin.options);
}
if (typeof plugin.help === 'string') {
command.help(plugin.help);
}
});
const options = parser.parse(process.argv.slice(2));
const browser = await puppeteer.launch({
headless : options.headless,
// TODO: add a verbose option
// dumpio : true,
executablePath : options.chromePath,
args : options.chromeArgs,
});
const page = await browser.newPage();
if (options.headers)
page.setExtraHTTPHeaders(options.headers)
await page.emulateMediaType('screen');
const pdf = await PDFDocument.create();
pdf.setCreator('Decktape');
if (options.metaAuthor)
pdf.setAuthor(options.metaAuthor);
if (options.metaSubject)
pdf.setSubject(options.metaSubject);
if (options.metaTitle)
pdf.setTitle(options.metaTitle);
page
.on('console', async msg => {
if (msg.type() === 'log') {
const args = await Promise.all(msg.args().map(arg => arg.evaluate(obj => obj, arg)));
console.log(...args.map(arg => color(msg.type())(util.format(arg))));
} else {
console.log(color(msg.type())(util.format(msg.text())));
}
})
.on('requestfailed', request => {
// do not output warning for cancelled requests
if (request.failure() && request.failure().errorText === 'net::ERR_ABORTED') return;
console.log(chalk.yellow('\nUnable to load resource from URL: %s'), request.url());
})
.on('pageerror', error => console.log(chalk.red('\nPage error: %s'), error.message));
console.log('Loading page', options.url, '...');
const load = page.waitForNavigation({ waitUntil: 'load', timeout: options.urlLoadTimeout });
page.goto(options.url, { waitUntil: 'networkidle0', timeout: options.pageLoadTimeout })
// wait until the load event is dispatched
.then(response => load
.catch(error => response.status() !== 200 ? Promise.reject(error) : response)
.then(_ => response))
// TODO: improve message when reading file locally
.then(response => console.log('Loading page finished with status: %s', response.status()))
.then(delay(options.loadPause))
.then(_ => createPlugin(page, plugins, options))
.then(plugin => configurePlugin(plugin)
.then(_ => configurePage(page, plugin, options))
.then(_ => exportSlides(page, plugin, pdf, options))
.then(async context => {
await writePdf(options.filename, pdf);
console.log(chalk.green(`\nPrinted ${chalk.bold('%s')} slides`), context.exportedSlides);
// Wait for the browser to close before exiting the process
await browser.close();
process.exit();
}))
.catch(async error => {
console.log(chalk.red('\n%s'), error);
// Wait for the browser to close before exiting the process
await browser.close();
process.exit(1);
});
})();
async function loadAvailablePlugins(pluginsPath) {
const plugins = await fs.promises.readdir(pluginsPath);
const entries = await Promise.all(plugins.map(async pluginPath => {
const [, plugin] = pluginPath.match(/^(.*)\.js$/);
if (plugin && (await fs.promises.stat(path.join(pluginsPath, pluginPath))).isFile()) {
return [plugin, await import(`./plugins/${pluginPath}`)];
}
}));
return Object.fromEntries(entries.filter(Boolean));
}
async function createPlugin(page, plugins, options) {
let plugin;
if (!options.command || options.command === 'automatic') {
plugin = await createActivePlugin(page, plugins, options);
if (!plugin) {
console.log('No supported DeckTape plugin detected, falling back to generic plugin');
plugin = plugins['generic'].create(page, options);
}
} else {
plugin = plugins[options.command].create(page, options);
if (!await plugin.isActive()) {
throw Error(`Unable to activate the ${plugin.getName()} DeckTape plugin for the address: ${options.url}`);
}
}
console.log(chalk.cyan(`${chalk.bold('%s')} plugin activated`), plugin.getName());
return plugin;
}
async function createActivePlugin(page, plugins, options) {
for (let id in plugins) {
if (id === 'generic') continue;
const plugin = plugins[id].create(page, options);
if (await plugin.isActive()) return plugin;
}
}
async function configurePage(page, plugin, options) {
if (!options.size) {
options.size = typeof plugin.size === 'function' ? await plugin.size() : { width: 1280, height: 720 };
}
await page.setViewport(options.size);
}
async function configurePlugin(plugin) {
if (typeof plugin.configure === 'function') {
await plugin.configure();
}
}
async function exportSlides(page, plugin, pdf, options) {
const context = {
progressBarOverflow : 0,
currentSlide : 1,
exportedSlides : 0,
pdfFonts : {},
pdfXObjects : {},
totalSlides : await plugin.slideCount(),
};
// TODO: support a more advanced "fragment to pause" mapping
// for special use cases like GIF animations
// TODO: support plugin optional promise to wait until a particular mutation
// instead of a pause
if (options.slides && !options.slides[context.currentSlide]) {
process.stdout.write('\r' + await progressBar(plugin, context, { skip: true }));
} else {
await pause(options.pause);
await exportSlide(page, plugin, pdf, context, options);
}
const maxSlide = options.slides ? Math.max(...Object.keys(options.slides)) : Infinity;
let hasNext = await hasNextSlide(plugin, context);
while (hasNext && context.currentSlide < maxSlide) {
await nextSlide(plugin, context);
await pause(options.pause);
if (options.slides && !options.slides[context.currentSlide]) {
process.stdout.write('\r' + await progressBar(plugin, context, { skip: true }));
} else {
await exportSlide(page, plugin, pdf, context, options);
}
hasNext = await hasNextSlide(plugin, context);
}
// Flush consolidated fonts
Object.values(context.pdfFonts).forEach(({ ref, font }) => {
pdf.context.assign(ref, pdf.context.flateStream(font.write({ type: 'ttf', hinting: true })));
});
return context;
}
async function exportSlide(page, plugin, pdf, context, options) {
process.stdout.write('\r' + await progressBar(plugin, context));
const buffer = await page.pdf({
width : options.size.width,
height : options.size.height,
printBackground : true,
pageRanges : '1',
displayHeaderFooter : false,
timeout : options.bufferTimeout,
});
await printSlide(pdf, await PDFDocument.load(buffer, { parseSpeed: ParseSpeeds.Fastest }), context);
context.exportedSlides++;
if (options.screenshots) {
for (let resolution of options.screenshotSizes || [options.size]) {
await page.setViewport(resolution);
// Delay page rendering to wait for the resize event to complete,
// e.g. for impress.js (may be needed to be configurable)
await pause(1000);
await page.screenshot({
path: path.join(options.screenshotDirectory, options.filename
.replace('.pdf', `_${context.currentSlide}_${resolution.width}x${resolution.height}.${options.screenshotFormat}`)),
fullPage: false,
omitBackground: true,
});
await page.setViewport(options.size);
await pause(1000);
}
}
}
async function printSlide(pdf, slide, context) {
const duplicatedEntries = [];
const [page] = await pdf.copyPages(slide, [0]);
pdf.addPage(page);
// Traverse the page to consolidate duplicates
parseResources(page.node);
// And delete all the collected duplicates
duplicatedEntries.forEach(ref => pdf.context.delete(ref));
function parseResources(dictionary) {
const resources = dictionary.get(PDFName.Resources);
if (resources.has(PDFName.XObject)) {
const xObject = resources.get(PDFName.XObject);
xObject.entries().forEach(entry => parseXObject(entry, xObject));
}
if (resources.has(PDFName.Font)) {
resources.get(PDFName.Font).entries().forEach(parseFont);
}
}
function parseXObject([name, entry], xObject) {
const object = page.node.context.lookup(entry);
const subtype = object.dict.get(PDFName.of('Subtype'));
if (subtype === PDFName.of('Image')) {
const digest = crypto.createHash('SHA1').update(object.contents).digest('hex');
const existing = context.pdfXObjects[digest];
if (!existing) {
// Store the entry that'll replace references with the same content
context.pdfXObjects[digest] = entry;
} else if (entry !== existing) {
// Only remove references from different pages
xObject.set(name, context.pdfXObjects[digest]);
duplicatedEntries.push(entry);
}
} else {
parseResources(object.dict);
}
};
function parseFont([_, entry]) {
const object = page.node.context.lookup(entry);
const subtype = object.get(PDFName.of('Subtype'));
// See "Introduction to Font Data Structures" from PDF specification
if (subtype === PDFName.of('Type0')) {
// TODO: properly support composite fonts with multiple descendants
const descendant = page.node.context.lookup(object.get(PDFName.of('DescendantFonts')).get(0));
if (descendant.get(PDFName.of('Subtype')) === PDFName.of('CIDFontType2')) {
const descriptor = page.node.context.lookup(descendant.get(PDFName.of('FontDescriptor')));
const ref = descriptor.get(PDFName.of('FontFile2'));
const file = page.node.context.lookup(ref);
if (!file) {
// The font has already been processed and removed
return;
}
const bytes = decodePDFRawStream(file).decode();
let font;
try {
// Some fonts written in the PDF may be ill-formed. Let's skip font compression in that case,
// until it's fixed in Puppeteer > Chromium > Skia.
// This happens for system fonts like Helvetica Neue for which cmap table is missing.
font = Font.create(Buffer.from(bytes), { type: fontType(bytes), hinting: true });
} catch (e) {
console.log(chalk.yellow('\nSkipping font compression: %s'), e.message);
return;
}
// Some fonts happen to miss some metadata and tables required by fonteditor
if (!font.data.name) {
font.data.name = {};
}
if (!font.data['OS/2']) {
font.data['OS/2'] = {};
}
// PDF font name does not contain sub family on Windows 10,
// so a more robust key is computed from the font metadata
const id = descriptor.get(PDFName.of('FontName')).value() + ' - ' + fontMetadataKey(font.data.name);
if (context.pdfFonts[id]) {
const f = context.pdfFonts[id].font;
font.data.glyf.forEach((g, i) => {
if (g.contours && g.contours.length > 0) {
if (!f.data.glyf[i] || !f.data.glyf[i].contours || f.data.glyf[i].contours.length === 0) {
mergeGlyph(f, i, g);
}
} else if (g.compound) {
if (!f.data.glyf[i] || typeof f.data.glyf[i].compound === 'undefined') {
mergeGlyph(f, i, g);
}
}
});
descriptor.set(PDFName.of('FontFile2'), context.pdfFonts[id].ref);
duplicatedEntries.push(ref);
} else {
context.pdfFonts[id] = { ref: ref, font: font };
}
}
}
};
function fontType(bytes) {
const buffer = Buffer.from(bytes);
if (buffer.readInt32BE() === 0x10000) {
return 'ttf';
}
if (buffer.toString('utf8', 0, 4) === 'OTTO') {
return 'otf';
}
}
function mergeGlyph(font, index, glyf) {
if (font.data.glyf.length <= index) {
for (let i = font.data.glyf.length; i < index; i++) {
font.data.glyf.push({ contours: Array(0), advanceWidth: 0, leftSideBearing: 0 });
}
font.data.glyf.push(glyf);
} else {
font.data.glyf[index] = glyf;
}
}
function fontMetadataKey(font) {
const keys = ['fontFamily', 'fontSubFamily', 'fullName', 'preferredFamily', 'preferredSubFamily', 'uniqueSubFamily'];
return Object.entries(font)
.filter(([key, _]) => keys.includes(key))
.reduce((r, [k, v], i) => r + (i > 0 ? ',' : '') + k + '=' + v, '');
}
}
async function hasNextSlide(plugin, context) {
if (typeof plugin.hasNextSlide === 'function') {
return await plugin.hasNextSlide();
} else {
return context.currentSlide < context.totalSlides;
}
}
async function nextSlide(plugin, context) {
context.currentSlide++;
return plugin.nextSlide();
}
async function writePdf(filename, pdf) {
const pdfDir = path.dirname(filename);
try {
fs.accessSync(pdfDir, fs.constants.F_OK);
} catch {
fs.mkdirSync(pdfDir, { recursive: true });
}
fs.writeFileSync(filename, await pdf.save({ addDefaultPage: false }));
}
// TODO: add progress bar, duration, ETA and file size
async function progressBar(plugin, context, { skip } = { skip: false }) {
const cols = [];
const index = await plugin.currentSlideIndex();
cols.push(`${skip ? 'Skipping' : 'Printing'} slide `);
cols.push(`#${index}`.padEnd(8));
cols.push(' (');
cols.push(`${context.currentSlide}`.padStart(context.totalSlides ? context.totalSlides.toString().length : 3));
cols.push('/');
cols.push(context.totalSlides || ' ?');
cols.push(') ...');
// erase overflowing slide fragments
cols.push(' '.repeat(Math.max(context.progressBarOverflow - Math.max(index.length + 1 - 8, 0), 0)));
context.progressBarOverflow = Math.max(index.length + 1 - 8, 0);
return cols.join('');
}