Stirling-PDF/src/main/resources/templates/other/compare.html
2023-05-08 22:55:01 +01:00

134 lines
6.8 KiB
HTML

<!DOCTYPE html>
<html th:lang="${#locale.toString()}" th:lang-direction="#{language.direction}" xmlns:th="http://www.thymeleaf.org">
<th:block th:insert="~{fragments/common :: head(title=#{repair.title})}"></th:block>
<body>
<div id="page-container">
<div id="content-wrap">
<div th:insert="~{fragments/navbar.html :: navbar}"></div>
<br> <br>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<h2 th:text="#{repair.header}"></h2>
<div th:replace="~{fragments/common :: fileSelector(name='fileInput', multiple=false, accept='application/pdf')}"></div>
<div th:replace="~{fragments/common :: fileSelector(name='fileInput2', multiple=false, accept='application/pdf')}"></div>
<button onclick="comparePDFs()">Compare</button>
<div class="row">
<div class="col-md-6">
<h3>Document 1</h3>
<div id="result1" class="result-column"></div>
</div>
<div class="col-md-6">
<h3>Document 2</h3>
<div id="result2" class="result-column"></div>
</div>
</div>
<style>
.result-column {
border: 1px solid #ccc;
padding: 15px;
overflow-y: scroll;
height: 400px;
white-space: pre-wrap;
}
</style>
<script>
async function comparePDFs() {
const file1 = document.getElementById("fileInput-input").files[0];
const file2 = document.getElementById("fileInput2-input").files[0];
if (!file1 || !file2) {
console.error("Please select two PDF files to compare");
return;
}
const [pdf1, pdf2] = await Promise.all([
pdfjsLib.getDocument(URL.createObjectURL(file1)).promise,
pdfjsLib.getDocument(URL.createObjectURL(file2)).promise
]);
const extractText = async (pdf) => {
const pages = [];
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
const content = await page.getTextContent();
const strings = content.items.map(item => item.str);
pages.push(strings.join(" "));
}
return pages.join(" ");
};
const [text1, text2] = await Promise.all([
extractText(pdf1),
extractText(pdf2)
]);
if (text1.trim() === "" || text2.trim() === "") {
alert("One or both of the selected PDFs have no text content. Please choose PDFs with text for comparison.");
return;
}
const diff = (text1, text2) => {
// ... Keep the same diff function from the previous response ...
};
const differences = diff(text1, text2);
const displayDifferences = (differences) => {
const resultDiv1 = document.getElementById("result1");
const resultDiv2 = document.getElementById("result2");
resultDiv1.innerHTML = "";
resultDiv2.innerHTML = "";
let doc1Pointer = 0;
let doc2Pointer = 0;
differences.forEach(([color, word]) => {
const span1 = document.createElement("span");
const span2 = document.createElement("span");
if (color === "green") {
span1.style.color = color;
span1.textContent = word;
resultDiv1.appendChild(span1);
doc1Pointer++;
} else if (color === "red") {
span2.style.color = color;
span2.textContent = word;
resultDiv2.appendChild(span2);
doc2Pointer++;
} else {
span1.style.color = color;
span1.textContent = word;
resultDiv1.appendChild(span1);
doc1Pointer++;
span2.style.color = color;
span2.textContent = word;
resultDiv2.appendChild(span2);
doc2Pointer++;
}
// Add space after each word
const space1 = document.createElement("span");
const space2 = document.createElement("span");
space1.textContent = " ";
space2.textContent = " ";
resultDiv1.appendChild(space1);
resultDiv2.appendChild(space2);
});
return result;
};
console.log('Differences:', differences);
displayDifferences(differences);
}
</script>
</div>
</div>
</div>
</div>
<div th:insert="~{fragments/footer.html :: footer}"></div>
</div>