-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
111 lines (90 loc) · 3.5 KB
/
Copy pathscript.js
File metadata and controls
111 lines (90 loc) · 3.5 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
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
import { TemplateHandler } from "https://cdn.jsdelivr.net/npm/easy-template-x/+esm";
let currentTags = [];
let currentTemplateFile = null;
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("extractTags").addEventListener("click", extractTags);
document.getElementById("generateFromForm").addEventListener("click", handleFormSubmission);
});
async function extractTags() {
const docxInput = document.getElementById("docxTemplate").files[0];
if (!docxInput) {
alert("Please upload a DOCX template.");
return;
}
currentTemplateFile = docxInput; // Save for later use
const reader = new FileReader();
reader.onload = async function (event) {
try {
const templateData = event.target.result;
const handler = new TemplateHandler();
const tags = await handler.parseTags(templateData);
currentTags = tags.map(tag => tag.rawText);
console.log("Extracted tags:", currentTags);
generateDynamicForm(currentTags);
} catch (error) {
console.error("Error extracting tags:", error);
alert("Failed to extract placeholders.");
}
};
reader.readAsArrayBuffer(docxInput);
}
function generateDynamicForm(tags) {
const formContainer = document.getElementById("dynamicForm");
formContainer.innerHTML = ""; // Clear previous form
tags.forEach(tag => {
const label = document.createElement("label");
label.textContent = `Enter value for "${tag}":`;
label.htmlFor = tag;
const input = document.createElement("input");
input.type = "text";
input.id = tag;
input.name = tag;
input.required = true;
formContainer.appendChild(label);
formContainer.appendChild(document.createElement("br"));
formContainer.appendChild(input);
formContainer.appendChild(document.createElement("br"));
formContainer.appendChild(document.createElement("br"));
});
}
async function handleFormSubmission(event) {
event.preventDefault();
if (!currentTemplateFile) {
alert("Please upload and extract tags from a DOCX template first.");
return;
}
const formData = new FormData(document.getElementById("dynamicForm"));
const dataObject = {};
currentTags.forEach(tag => {
dataObject[tag] = formData.get(tag) || "";
});
processTemplate(currentTemplateFile, dataObject);
}
async function processTemplate(docxFile, dataObject) {
const reader = new FileReader();
reader.onload = async function (event) {
try {
const templateData = event.target.result;
const handler = new TemplateHandler();
const outputDoc = await handler.process(templateData, dataObject);
const blob = new Blob([outputDoc], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
saveFile("ProcessedDocument.docx", blob);
} catch (error) {
console.error("Error processing document:", error);
alert("Failed to process document.");
}
};
reader.readAsArrayBuffer(docxFile);
}
function saveFile(filename, blob) {
const blobUrl = URL.createObjectURL(blob);
const link = document.createElement("a");
link.download = filename;
link.href = blobUrl;
document.body.appendChild(link);
link.click();
setTimeout(() => {
link.remove();
window.URL.revokeObjectURL(blobUrl);
}, 0);
}