-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdfgen.html
62 lines (59 loc) · 1.84 KB
/
pdfgen.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
</head>
<body>
<img id="image" alt="">
<div id="content"></div>
<button id="btn">button</button>
<button onclick="downloadpdf()">download</button>
</body>
<script>
const image = document.getElementById('image');
const content = document.getElementById('content');
let imageData = '';
let contentText = '';
document.getElementById('btn').addEventListener('click', () => {
fetch('http://localhost:8080/', {
method: 'GET',
headers: {
"content-type": "application/json"
}
})
.then(res => res.json())
.then(data => {
imageData = data.obj.image;
contentText = data.obj.content;
console.log(data)
image.src = data.obj.image;
content.textContent = data.obj.content;
})
.catch(err => {
console.log(err);
});
});
function downloadpdf() {
const doc = new jsPDF();
doc.text(contentText, 10, 10);
if (imageData) {
fetch(imageData)
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.onload = function() {
doc.addImage(this.result, 'JPEG', 10, 20, 180, 160);
doc.save('document.pdf');
};
reader.readAsDataURL(blob);
})
.catch(err => console.log(err));
} else {
doc.save('document.pdf');
}
}
</script>
</html>