-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprintImage.js
More file actions
72 lines (59 loc) · 2.82 KB
/
Copy pathprintImage.js
File metadata and controls
72 lines (59 loc) · 2.82 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
//node print.js /documentation/Specimen.html
//node printImage.js documentation/Image.html fonts/ttf/Wittgenstein-Regular.ttf documentation/Image.pdf
const opentype = require('opentype.js');
const { execSync } = require('child_process');
var puppeteer = require('puppeteer');
var INFILE = "/" + process.argv[2];
var PNGFILE = "./" + process.argv[4]; //
if( process.argv[4] == undefined ){
PNGFILE = "./" + process.argv[2] + '.png';
}
var FONTFILE = "../" + process.argv[3]; //fonts/ttf/Wittgenstein-SemiBoldItalic.ttf";
console.log( "print " + INFILE + " " + FONTFILE + " " + PNGFILE + " " );
//console.log( "output to", PDFFILE );
( async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('file://' + process.cwd() + INFILE, { 'waitUntil' : 'domcontentloaded'});
await page.addStyleTag({content: '@font-face{font-family: TESTFONT; src: url( ' + FONTFILE + '); }'})
const font = opentype.parse(require("fs").readFileSync( process.argv[3]));
//console.log( "Font loaded", font.names );
// Constants that are worked out dynamically
const MY_URL = execSync("git remote get-url origin").toString().trim();
const MY_HASH = execSync("git rev-parse --short HEAD").toString().trim();
const FONT_NAME = font.names.windows.fullName.en;
const FONT_VERSION = `v${font.tables.head.fontRevision.toFixed(3)}`;
console.log (MY_URL,MY_HASH, FONT_NAME, FONT_VERSION );
// .fontname::before{ content: "Fontname"; }
await page.addStyleTag({content: '.MY_URL::before{ content: "' + MY_URL + ' at commit ' + MY_HASH + '"; }'})
await page.addStyleTag({content: '.FONT_NAME::before{ content: "' + FONT_NAME + '"; }'})
await page.addStyleTag({content: '.FONT_VERSION::before{ content: "' + FONT_VERSION + '"; }'})
await delay(500);
/*
await page.emulateMediaType('print');
await page.pdf({
path: PDFFILE,
printBackground: true,
preferCSSPageSize: true,
displayHeaderFooter: true,
headerTemplate: '<html></html>',
footerTemplate:
`
<div style="width: 100%; height: 8mm; text-align: center; font-family: Times; font-size: 8pt;">
` + process.argv[2] + ` - ` + process.argv[3] +
` - <span class='date''></span> - <span class='pageNumber''></span> / <span class='totalPages'></span>
</div>
`
//<span class='url''></span>
})
*/
//2048px 1024px;
await page.setViewport({ width: 2048, height: 1024, deviceScaleFactor: 1 });
await page.screenshot({ path: PNGFILE });
await browser.close();
})();
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}