-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (70 loc) · 2.73 KB
/
script.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
//Before Running this Script
//Run this with Console Importer
//$i('https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js')
var lecturetitle = document.getElementById('recording-title').innerHTML;
var thumbnail = document.getElementsByClassName('thumbnail-wrapper');
var imageUrls = new Array();
var x = 0;
var dict = [];
var maxheight = 0;
var maxwidth = 0;
for (var i = 0; i < thumbnail.length; i++) {
var thumbnailDiv = thumbnail[i];
var image = thumbnailDiv.getElementsByTagName("img")[0];
var imgname = image.src.replace(/^.*[\\\/]/, '');
var source = image.src;
var curr_ht = image.naturalHeight;
var curr_wd = image.naturalWidth;
if(!(imgname in dict))
{
imageUrls[x] = source;
dict[imgname] = source;
x++;
}
maxheight = Math.max(maxheight, curr_ht);
maxwidth = Math.max(maxwidth, curr_wd);
}
console.log(dict);
function pix2mm(val){
return (val*0.2645833333);
}
async function savePdf() {
const multiPng = await generatePdf(imageUrls);
multiPng.save(lecturetitle);
alert("Downloading " + lecturetitle + ".pdf");
}
async function addImageProcess(src) {
return new Promise((resolve, reject) => {
let img = new Image();
img.src = src;
img.onload = () => resolve(img);
img.onerror = reject;
});
}
async function generatePdf(imageUrls) {
var orientation;
if(maxwidth >= maxheight)
{
orientation = 'landscape';
}
else if ( maxwidth < maxheight)
{
orientation = 'portrait'
}
var width_mm = pix2mm(maxwidth);
var height_mm = pix2mm(maxwidth);
const doc = new jsPDF(orientation, 'mm', [width_mm, height_mm], true);
for (const [i, url] of imageUrls.entries()) {
const image = await addImageProcess(url);
var filename = image.src.replace(/^.*[\\\/]/, '');
console.log("Fetching " + filename);
img_ht = pix2mm(image.naturalHeight);
img_wt = pix2mm(image.naturalWidth);
doc.addImage(image, "png", 0, 0, img_wt, img_ht, null, 'FAST' );
if (i !== imageUrls.length - 1) {
doc.addPage();
}
}
return doc;
}
savePdf().then(console.log("Saved"));