1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-09-21 04:10:38 +02:00

Further Fixes

This commit is contained in:
Anthony Stirling 2023-07-26 22:08:19 +01:00
parent bf995f989c
commit 33a6a7869c
5 changed files with 45 additions and 60 deletions

View File

@ -8,7 +8,7 @@ plugins {
} }
group = 'stirling.software' group = 'stirling.software'
version = '0.11.1' version = '0.11.2'
sourceCompatibility = '17' sourceCompatibility = '17'
repositories { repositories {

View File

@ -43,7 +43,7 @@ public class ConvertImgPDFController {
@Parameter(description = "Choose between a single image containing all pages or separate images for each page", schema = @Schema(allowableValues = {"single", "multiple"})) @Parameter(description = "Choose between a single image containing all pages or separate images for each page", schema = @Schema(allowableValues = {"single", "multiple"}))
String singleOrMultiple, String singleOrMultiple,
@RequestParam("colorType") @RequestParam("colorType")
@Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"rgb", "greyscale", "blackwhite"})) @Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"color", "greyscale", "blackwhite"}))
String colorType, String colorType,
@RequestParam("dpi") @RequestParam("dpi")
@Parameter(description = "The DPI (dots per inch) for the output image(s)") @Parameter(description = "The DPI (dots per inch) for the output image(s)")
@ -94,7 +94,7 @@ public class ConvertImgPDFController {
@Parameter(description = "Whether to stretch the images to fit the PDF page or maintain the aspect ratio", example = "false") @Parameter(description = "Whether to stretch the images to fit the PDF page or maintain the aspect ratio", example = "false")
boolean stretchToFit, boolean stretchToFit,
@RequestParam("colorType") @RequestParam("colorType")
@Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"rgb", "greyscale", "blackwhite"})) @Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"color", "greyscale", "blackwhite"}))
String colorType, String colorType,
@RequestParam(defaultValue = "false", name = "autoRotate") @RequestParam(defaultValue = "false", name = "autoRotate")
@Parameter(description = "Whether to automatically rotate the images to better fit the PDF page", example = "true") @Parameter(description = "Whether to automatically rotate the images to better fit the PDF page", example = "true")

View File

@ -1,35 +1,25 @@
package stirling.software.SPDF.controller.web; package stirling.software.SPDF.controller.web;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Hidden;
@ -128,45 +118,28 @@ public class GeneralWebController {
model.addAttribute("fonts", getFontNames()); model.addAttribute("fonts", getFontNames());
return "sign"; return "sign";
} }
@Autowired
private ResourceLoader resourceLoader;
private List<String> getFontNames() { private List<String> getFontNames() {
List<String> fontNames = new ArrayList<>();
try { try {
// Get the directory URL from classpath Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
URL dirURL = getClass().getClassLoader().getResource("static/fonts"); .getResources("classpath:static/fonts/*.woff2");
if (dirURL != null && dirURL.getProtocol().equals("file")) { return Arrays.stream(resources)
// If running from the file system (e.g., IDE) .map(resource -> {
fontNames.addAll( try {
Files.list(Paths.get(dirURL.toURI())) String filename = resource.getFilename();
.map(java.nio.file.Path::getFileName) return filename.substring(0, filename.length() - 6); // Remove .woff2 extension
.map(java.nio.file.Path::toString) } catch (Exception e) {
.filter(name -> name.endsWith(".woff2")) throw new RuntimeException("Error processing filename", e);
.map(name -> name.substring(0, name.length() - 6)) // Remove .woff2 extension
.collect(Collectors.toList())
);
} else {
// If running from a JAR file
// Resources in JAR go through a different URL protocol.
// In this case, we'll use a different approach to list them.
// Create a link to the resource. This assumes resources are at the root of the JAR.
URI uri = getClass().getResource("/").toURI();
FileSystem fileSystem = FileSystems.newFileSystem(uri, new HashMap<>());
Path myPath = fileSystem.getPath("/static/fonts/");
Files.walk(myPath, 1)
.filter(path -> !Files.isDirectory(path))
.map(path -> path.getFileName().toString())
.filter(name -> name.endsWith(".woff2"))
.map(name -> name.substring(0, name.length() - 6)) // Remove .woff2 extension
.forEach(fontNames::add);
fileSystem.close();
} }
} catch (IOException | URISyntaxException e) { })
.collect(Collectors.toList());
} catch (Exception e) {
throw new RuntimeException("Failed to read font directory", e); throw new RuntimeException("Failed to read font directory", e);
} }
return fontNames;
} }

View File

@ -155,10 +155,21 @@ async function submitMultiPdfForm(url, files) {
jszip = new JSZip(); jszip = new JSZip();
} }
// Get the form with the method attribute set to POST
let postForm = document.querySelector('form[method="POST"]');
// Get existing form data // Get existing form data
let formData = new FormData($('form')[0]); let formData;
if (postForm) {
formData = new FormData($(postForm)[0]); // Convert the form to a jQuery object and get the raw DOM element
} else {
console.log("No form with POST method found.");
}
//Remove file to reuse parameters for other runs
formData.delete('fileInput'); formData.delete('fileInput');
const CONCURRENCY_LIMIT = 8; const CONCURRENCY_LIMIT = 8;
const chunks = []; const chunks = [];
for (let i = 0; i < Array.from(files).length; i += CONCURRENCY_LIMIT) { for (let i = 0; i < Array.from(files).length; i += CONCURRENCY_LIMIT) {
@ -169,10 +180,11 @@ async function submitMultiPdfForm(url, files) {
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);
// Add other form data // 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]);
} }
try { try {

View File

@ -31,7 +31,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label th:text="#{pdfToImage.colorType}"></label> <label th:text="#{pdfToImage.colorType}"></label>
<select class="form-control" name="colorType"> <select class="form-control" id="colorType" name="colorType">
<option value="color" th:text="#{pdfToImage.color}"></option> <option value="color" th:text="#{pdfToImage.color}"></option>
<option value="greyscale" th:text="#{pdfToImage.grey}"></option> <option value="greyscale" th:text="#{pdfToImage.grey}"></option>
<option value="blackwhite" th:text="#{pdfToImage.blackwhite}"></option> <option value="blackwhite" th:text="#{pdfToImage.blackwhite}"></option>