Merge pull request #2493 from reecebrowne/bug/2490/2488/image-to-pdf

Img to pdf bug fixes
This commit is contained in:
Anthony Stirling 2024-12-18 10:42:34 +00:00 committed by GitHub
commit 9a6afdd921
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 9 deletions

View File

@ -208,7 +208,13 @@ public class ConvertImgPDFController {
String fitOption = request.getFitOption(); String fitOption = request.getFitOption();
String colorType = request.getColorType(); String colorType = request.getColorType();
boolean autoRotate = request.isAutoRotate(); boolean autoRotate = request.isAutoRotate();
// Handle Null entries for formdata
if (colorType == null || colorType.isBlank()) {
colorType = "color";
}
if (fitOption == null || fitOption.isEmpty()) {
fitOption = "fillPage";
}
// Convert the file to PDF and get the resulting bytes // Convert the file to PDF and get the resulting bytes
byte[] bytes = byte[] bytes =
PdfUtils.imageToPdf(file, fitOption, autoRotate, colorType, pdfDocumentFactory); PdfUtils.imageToPdf(file, fitOption, autoRotate, colorType, pdfDocumentFactory);

View File

@ -415,8 +415,9 @@
const promises = chunk.map(async (file) => { const promises = chunk.map(async (file) => {
let fileFormData = new FormData(); let fileFormData = new FormData();
fileFormData.append('fileInput', file); fileFormData.append('fileInput', file);
console.log(fileFormData); for (let [key, value] of fileFormData.entries()) {
// Add other form data console.log(key, value);
} // Add other form data
for (let pair of formData.entries()) { for (let pair of formData.entries()) {
fileFormData.append(pair[0], pair[1]); fileFormData.append(pair[0], pair[1]);
console.log(pair[0] + ', ' + pair[1]); console.log(pair[0] + ', ' + pair[1]);

View File

@ -222,7 +222,7 @@ async function downloadPDF() {
// Event listeners // Event listeners
document.getElementById('fileInput-input').addEventListener('change', function (e) { document.getElementById('fileInput-input').addEventListener('change', function (e) {
const fileInput = event.target; const fileInput = e.target;
fileInput.addEventListener('file-input-change', async (e) => { fileInput.addEventListener('file-input-change', async (e) => {
const {allFiles} = e.detail; const {allFiles} = e.detail;
if (allFiles && allFiles.length > 0) { if (allFiles && allFiles.length > 0) {

View File

@ -17,7 +17,7 @@
<span class="material-symbols-rounded tool-header-icon image">image</span> <span class="material-symbols-rounded tool-header-icon image">image</span>
<span class="tool-header-text" th:text="#{imageToPDF.header}"></span> <span class="tool-header-text" th:text="#{imageToPDF.header}"></span>
</div> </div>
<form method="post" enctype="multipart/form-data" th:action="@{'/api/v1/convert/img/pdf'}"> <form id="imageToPDFForm" method="post" enctype="multipart/form-data" th:action="@{'/api/v1/convert/img/pdf'}">
<div th:replace="~{fragments/common :: fileSelector(name='fileInput', multipleInputsForSingleRequest=false, accept='image/*', inputText=#{imgPrompt})}"></div> <div th:replace="~{fragments/common :: fileSelector(name='fileInput', multipleInputsForSingleRequest=false, accept='image/*', inputText=#{imgPrompt})}"></div>
<div class="mb-3"> <div class="mb-3">
<label for="fitOption" th:text="#{imageToPDF.selectLabel}">Fit Options</label> <label for="fitOption" th:text="#{imageToPDF.selectLabel}">Fit Options</label>
@ -51,11 +51,18 @@
<br> <br>
<button type="submit" id="submitBtn" class="btn btn-primary" th:text="#{imageToPDF.submit}"></button> <button type="submit" id="submitBtn" class="btn btn-primary" th:text="#{imageToPDF.submit}"></button>
<script> <script>
$('#fileInput-input').on('file-input-change', () => { $('#fileInput-input').on('change', function(e) {
var files = document.getElementById("fileInput-input").files; const fileInput = e.target;
fileInput.addEventListener('file-input-change', async (e) => {
const {allFiles} = e.detail;
var conversionType = document.getElementById("conversionType"); var conversionType = document.getElementById("conversionType");
console.log("files.length=" + files.length) console.log("files.length=" + allFiles.length)
conversionType.disabled = files.length <= 1; if (allFiles.length > 1) {
conversionType.disabled = false;
} else {
conversionType.disabled = true;
}
});
}); });
$('#conversionType').change(function() { $('#conversionType').change(function() {
@ -68,6 +75,38 @@
override.value = "multi"; override.value = "multi";
} }
}); });
document.getElementById("imageToPDFForm").addEventListener("submit", async function (e) {
e.preventDefault();
const form = e.target;
const formData = new FormData(form);
const fitOptionSelect = document.getElementById("fitOption");
const fitOptionValue = fitOptionSelect ? fitOptionSelect.value : "fit-to-page"; // Default value
formData.append("fitOption", fitOptionValue);
const overrideInput = document.getElementById("override");
console.log("Override value before submission:", overrideInput.value);
for (let [key, value] of formData.entries()) {
console.log(`${key}:`, value);
}
try {
const response = await fetch(form.action, {
method: "POST",
body: formData
});
if (response.ok) {
console.log("Form successfully submitted");
} else {
console.error("Failed to submit the form");
}
} catch (error) {
console.error("Error submitting the form:", error);
}
});
</script> </script>
</form> </form>
</div> </div>