-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
96 lines (82 loc) · 2.6 KB
/
index.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
const axios = require('axios');
const afterLoad = require('after-load');
const PdfPrinter = require('pdfmake');
const fs = require('fs');
let globalConfig = {
dir: './documents', // destination directory
fonts: {
Roboto: {
normal: './fonts/Roboto-Regular.ttf',
bold: './fonts/Roboto-Medium.ttf',
italics: './fonts/Roboto-Italic.ttf',
bolditalics: './fonts/Roboto-MediumItalic.ttf'
}
}, // fonts for pdfmake
getTitle: url => url.replace(/\//g, '_').replace(/[:/.]/g, '') // default title
};
const checkRequiredConfig = config => {
if (!config.urls) {
throw new Error('Missing url list.');
}
if (!config.getImagesHref) {
throw new Error('Missing getImagesHref function.');
}
};
const getUrlContent = url => {
console.log(`Getting HTML from ${url}.`);
const title = globalConfig.getTitle(url);
const pdfConfig = {
title,
fonts: globalConfig.fonts,
url,
path: `${globalConfig.dir}/${title}.pdf`
};
return new Promise(resolve => {
afterLoad(url, async (html, $) => {
const images = globalConfig
.getImagesHref(html, $)
.filter(img => img)
.map(href => (!href.startsWith('http') ? url + href : href));
await downloadImagesToPdf(images, pdfConfig);
resolve();
});
});
};
const downloadImagesToPdf = async (images, pdfConfig) => {
const promises = images.map(img => axios.get(img, { responseType: 'arraybuffer' }));
await Promise.all(promises).then(values => {
const base64 = values.map(
val => 'data:image/jpeg;base64,' + Buffer.from(val.data, 'binary').toString('base64')
);
pdfConfig.docDefinition = {
content: [
{ text: pdfConfig.url, link: pdfConfig.url },
...base64.map(img => ({ image: img, width: 612.0 }))
],
pageMargins: [0, 0, 0, 0]
};
createPdf(pdfConfig);
});
};
const createPdf = pdfConfig => {
const printer = new PdfPrinter(pdfConfig.fonts);
const pdfDoc = printer.createPdfKitDocument(pdfConfig.docDefinition);
pdfDoc.pipe(fs.createWriteStream(pdfConfig.path));
pdfDoc.end();
console.log(`Content for ${pdfConfig.title} ready in '${pdfConfig.path}'!`);
};
const downloader = async config => {
checkRequiredConfig(config);
globalConfig = { ...globalConfig, ...config };
if (!fs.existsSync(globalConfig.dir)) {
fs.mkdirSync(globalConfig.dir);
}
const promises = globalConfig.urls.map(getUrlContent);
await Promise.all(promises);
console.log(
`Images for all ${globalConfig.urls.length} urls have been downloaded. You can find them in '${
globalConfig.dir
}'`
);
};
module.exports = downloader;