-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
163 lines (144 loc) · 4.47 KB
/
test.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo for html-css-export-word</title>
<link rel="icon" type="image/x-icon" href="lawFavicon.ico">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
height: 100vh;
display: flex;
flex-direction: column;
}
.container {
display: flex;
width: 100%;
flex-wrap: wrap;
}
.vertical-container {
display: flex;
flex-direction: column;
flex: 1 0 33.33%;
max-width: 33.33%;
box-sizing: border-box;
}
textarea,
iframe {
margin-top: 0%;
height: 500px;
font-size: large;
border: 4mm ridge black;
box-sizing: border-box;
}
p {
margin-top: 10px;
margin-bottom: 10px;
font-size: x-large;
font-weight: bold;
}
input {
margin-top: 10px;
font-size: x-large;
}
button {
text-align: left;
font-weight: bold;
margin-top: 10px;
padding: 10px;
background-color: black;
color: white;
border: none;
font-size: x-large;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<div class="vertical-container">
<p>HTML</p>
<textarea id="htmlInput"><div><h1>Welcome to My Page</h1><p>This is a sample page created using HTML and CSS.</p></div>
</textarea>
</div>
<div class="vertical-container">
<p>CSS</p>
<textarea
id="cssInput">body { font-family: Arial, sans-serif; background-color: #f0f0f0; } div{width: 80%; margin: 20px auto; background-color: #fef; padding: 20px;} h1 { color: #951; } p { color: #159; }</textarea>
</div>
<div class="vertical-container">
<p>Result</p>
<iframe id="result"></iframe>
</div>
</div>
<input type="text" placeholder="Enter name of Word document (optional)">
<button>Download Word document</button>
<script src="
https://cdn.jsdelivr.net/npm/[email protected]/dist/FileSaver.min.js
"></script>
<script>
function htmlCssExportWord(
html,
styles = "",
filename = "exported-document.doc"
) {
const statiC = {
mhtml: {
top:
"Mime-Version: 1.0\nContent-Base: " +
window.location.href +
'\nContent-Type: Multipart/related; boundary="NEXT.ITEM-BOUNDARY";type="text/html"\n\n--NEXT.ITEM-BOUNDARY\nContent-Type: text/html; charset="utf-8"\nContent-Location: ' +
window.location.href +
"\n\n<!DOCTYPE html>\n<html>\n_html_</html>",
head: '<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n<style>\n_styles_\n</style>\n</head>\n',
body: "<body>_body_</body>",
},
}
// Prepare bottom of mhtml file with image data
const mhtmlBottom = "\n" + "--NEXT.ITEM-BOUNDARY--"
// Aggregate parts of the file together
const fileContent =
statiC.mhtml.top.replace(
"_html_",
statiC.mhtml.head.replace("_styles_", styles) +
statiC.mhtml.body.replace("_body_", html)
) + mhtmlBottom
// Create a Blob with the file contents
const blob = new Blob([fileContent], {
type: "application/msword;charset=utf-8",
})
saveAs(blob, filename)
}
const htmlInput = document.getElementById("htmlInput")
const cssInput = document.getElementById("cssInput")
const result = document.getElementById("result").contentWindow.document
const input = document.querySelector("input")
const button = document.querySelector("button")
function updateResult() {
result.open()
result.write(
`<html><head><style>${cssInput.value}</style></head><body>${htmlInput.value}</body></html>`
)
result.close()
}
htmlInput.addEventListener("input", updateResult)
cssInput.addEventListener("input", updateResult)
button.addEventListener("click", () => {
if (isMobile) {
alert("The download to Word function is designed for desktop use only.")
return
}
htmlCssExportWord(htmlInput.value, cssInput.value, input.value)
})
window.onload = updateResult()
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
if (isMobile) {
alert("The download to Word function is designed for desktop use only.")
}
</script>
</body>
</html>