Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (53 loc) · 2.03 KB
/
Copy pathscript.js
File metadata and controls
70 lines (53 loc) · 2.03 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
let trustedURL;
if (window.trustedTypes && trustedTypes.createPolicy) {
const policy = trustedTypes.createPolicy('myPolicy', {
createScriptURL: (input) => {
return input;
}
});
trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js');
} else {
trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js';
}
let jspdf = document.createElement("script");
jspdf.onload = function() {
let pdf;
let elements = document.getElementsByTagName("img");
for (let i = 0; i < elements.length; i++) {
let img = elements[i];
if (!/^blob:/.test(img.src)) {
continue;
}
let canvasElement = document.createElement('canvas');
let con = canvasElement.getContext("2d");
canvasElement.width = img.width;
canvasElement.height = img.height;
con.drawImage(img, 0, 0, img.width, img.height);
let imgData = canvasElement.toDataURL("image/jpeg", 1.0);
// Determine the orientation based on the aspect ratio
let orientation = (img.width > img.height) ? 'landscape' : 'portrait';
if (i === 0) {
pdf = new jsPDF(orientation);
} else {
pdf.addPage(orientation);
}
let pageWidth = pdf.internal.pageSize.width;
let pageHeight = pdf.internal.pageSize.height;
let imgAspect = img.width / img.height;
let pageAspect = pageWidth / pageHeight;
let imgWidth, imgHeight;
if (imgAspect > pageAspect) {
imgWidth = pageWidth;
imgHeight = pageWidth / imgAspect;
} else {
imgHeight = pageHeight;
imgWidth = pageHeight * imgAspect;
}
let x = (pageWidth - imgWidth) / 2;
let y = (pageHeight - imgHeight) / 2;
pdf.addImage(imgData, 'JPEG', x, y, imgWidth, imgHeight);
}
pdf.save("download.pdf");
};
jspdf.src = trustedURL;
document.body.appendChild(jspdf);