-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
124 lines (123 loc) · 4.03 KB
/
index.html
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
112
113
114
115
116
117
118
119
120
121
122
123
124
<html>
<head>
<style type="text/css">
.imgclass {
max-width: 10rem;
border: solid black 1px;
}
.text-center
{
text-align: center;
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<table><tbody>
<tr>
<td>
<img id="page-0" src="./page-0.jpg" class="imgclass"> <br> <span class="text-center">page-0.jpg</span>
</td>
<td>
<img id="page-1" src="./page-1.jpg" class="imgclass"> <br> <span class="text-center">page-1.jpg</span>
</td>
<td>
<img id="page-2" src="./page-2.jpg" class="imgclass"> <br> <span class="text-center">page-2.jpg</span>
</td>
<td>
<img id="page-3" src="./page-3.jpg" class="imgclass"> <br> <span class="text-center">page-3.jpg</span>
</td>
</tr>
</tbody></table>
<br>
<button onclick="extractPDF();"> Download Extract</button>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js"></script>
<script>
function extractPDF() {
//init doc var to null
let doc = null;
//define wich page have which orientation
let fileorientation = [0, 90, 180, 270];
//Function to return 'l' or 'p' relative to the library to indicate if the page is in landscape mode or while added with the "addPage" function
let isLandscape = indexpage => {
return fileorientation[indexpage] == 90 ||
fileorientation[indexpage] == 270
? "l"
: "p";
};
//init widht and height to 0
let width = 0;
let height = 0;
//for each page of the html document above
for (var indexpage = 0; indexpage < 4; indexpage++) {
//get the img into the HTML code
let element = document.querySelector("#page-" + indexpage);
//call the function above to check if the page is vertical or horizontal (landscape)
let orientation = isLandscape(indexpage);
//if its first page we need create the document else to add a page to the document
if (indexpage == 0) {
doc = new jsPDF({
orientation: orientation
});
} else {
doc.addPage(null, orientation);
}
//console.log('orientation' + orientation);
//get the current page width and page height of the document
width = doc.internal.pageSize.getWidth();
height = doc.internal.pageSize.getHeight();
//if the page is in portrait
if (orientation == "p") {
//if the page is on angle 0 then add the page without rotation
if (fileorientation[indexpage] == 0)
doc.addImage(element, "JPEG", 0, 0, width, height);
//if the page is on angle 90 then add the page with rotation of 180 deg
else if (fileorientation[indexpage] == 180)
doc.addImage(
element,
"JPEG",
width,
-height,
width,
height,
null,
null,
-180
);
//if the page is in landscape mode
} else if (orientation == "l") {
//if the page is on angle 90 then add the page with rotation of 90 deg
if (fileorientation[indexpage] == 90)
doc.addImage(
element,
"JPEG",
0,
-width,
height,
width,
null,
null,
-90
);
//if the page is on angle 90 then add the page with rotation of 270 deg
else if (fileorientation[indexpage] == 270)
doc.addImage(
element,
"JPEG",
width,
-(width - height),
height,
width,
null,
null,
-270
);
}
}
//save the pdf
doc.save("result.pdf");
}
</script>
</html>