-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
111 lines (77 loc) · 2.18 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const puppeteer = require('puppeteer');
const fs = require('fs');
const PDFMerger = require('pdf-merger-js');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const url = 'file:///C:/PATH/TO/THE/EXPORTED/HTML/FILE.html';
let depth = 1;
let printed = [];
const printPdf = async (url) => {
console.log('Generating PDF for: ' + url);
if(depth >= 100){
//Failsafe
return;
}
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
page.setViewport({
width: 1920,
height: 1080
});
await page.goto(url, {
waitUntil: 'networkidle2'
});
const hrefs = await page.$$eval('a', as => as.map(a => a.href));
for(let href of hrefs){
if(href.indexOf("file://") == 0){
if(printed.indexOf(href) != -1){
console.log("Already visited " + href + "! Skipping...");
continue;
}
printed.push(href);
let newUrl = href;
let extensionSplit = newUrl.split(".");
if(extensionSplit[1].trim() != "html"){
continue;
}
let thisDepth = depth;
depth++;
const newBuff = await(printPdf(newUrl));
writeBufferToFile(newBuff, thisDepth+".pdf");
}
}
const pdfFile = await page.pdf({
format: 'A4',
printBackground: true,
displayHeaderFooter: true,
footerTemplate: " "
});
await browser.close();
return pdfFile;
};
function writeBufferToFile(buffer, file){
fs.writeFile("./out/"+file, buffer, "binary",function(err) { console.error(err); });
}
async function mergeAllPDF(){
console.log("Merging PDF");
let merger = new PDFMerger();
files = await readdir("./out");
files.forEach(function (file) {
console.log("Adding " + file + " to merge");
merger.add('./out/'+file); //merge all pages. parameter is the path to file and filename.
});
await merger.save('Export.pdf'); //save under given name
console.log("PDF Saved!");
console.log("Cleanup started");
files.forEach(function (file) {
console.log("Deleting " + file + "!");
fs.unlinkSync("./out/"+file);
});
}
(async() => {
const buffer = await printPdf(url);
writeBufferToFile(buffer, "0.pdf");
await mergeAllPDF();
})();