-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
121 lines (108 loc) · 4.44 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>mswordGen</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" />
<link
rel="icon"
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>
⚪️
</text></svg>" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
<script type="text/javascript" src="notificationToast.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/docx@8.2.4/build/index.umd.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js"></script>
</head>
<body>
<header>
<h1>Microsoft Word File (.docx) Generator</h1>
<p>This website lets you generate a MS-Word document from a bunch of images.</p>
<p>To get started, just upload your images below - and you're good to go!</p>
</header>
<main>
<input type="file" class="file-input" name="filefield" multiple="multiple" accept="image/*" />
</main>
</body>
<script>
// https://docx.js.org/#/
function processImages(images) {
const doc = new docx.Document({
sections: [],
})
// width and height of image are 'unknown', must be read from binary
const imagePromises = images.map((image) => {
return new Promise((resolve, reject) => {
const img = new Image()
img.src = URL.createObjectURL(image)
img.onload = () => {
console.log(`Width x Height: ${img.width} x ${img.height}`)
// scale based on a4 paper size
const MAX_WIDTH = 595
if (img.width > MAX_WIDTH) {
img.height *= MAX_WIDTH / img.width
img.width = MAX_WIDTH
}
// create node in docx
const imageParagraph = new docx.Paragraph({
children: [
new docx.ImageRun({
data: image,
transformation: {
width: img.width,
height: img.height,
},
}),
],
})
doc.addSection({
children: [imageParagraph],
})
resolve()
}
img.onerror = reject
})
})
// save
Promise.all(imagePromises)
.then(() => {
docx.Packer.toBlob(doc).then((blob) => {
saveAs(blob, 'example.docx')
NotificationToast.success('Document created successfully')
})
})
.catch((error) => {
console.error('Error processing images:', error)
NotificationToast.error('Error processing images')
})
}
window.onload = async () => {
const fileInput = document.querySelector('.file-input')
fileInput.addEventListener('change', (e) => {
const numFiles = e.target.files.length
if (numFiles === 0) {
return
}
NotificationToast.success(`You have selected ${numFiles} files.`)
const files = Array.from(e.target.files)
processImages(files)
})
}
</script>
<style>
/* upload field */
input[type='file'] {
margin-top: 2rem;
}
/* upload button */
::-webkit-file-upload-button {
margin-right: 1rem;
border-radius: 50%;
width: 2rem;
height: 2rem;
border: none;
color: transparent;
}
</style>
</html>