mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-11-01 01:21:18 +01:00 
			
		
		
		
	split fixes
This commit is contained in:
		
							parent
							
								
									4fbfd0bae4
								
							
						
					
					
						commit
						76e6a23674
					
				@ -118,7 +118,7 @@ public class SplitPdfBySectionsController {
 | 
				
			|||||||
                            new PDPageContentStream(subDoc, subPage)) {
 | 
					                            new PDPageContentStream(subDoc, subPage)) {
 | 
				
			||||||
                        // Set clipping area and position
 | 
					                        // Set clipping area and position
 | 
				
			||||||
                        float translateX = -subPageWidth * i;
 | 
					                        float translateX = -subPageWidth * i;
 | 
				
			||||||
                        float translateY = height - subPageHeight * (verticalDivisions - j);
 | 
					                        float translateY = -subPageHeight * (verticalDivisions - 1 - j);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        contentStream.saveGraphicsState();
 | 
					                        contentStream.saveGraphicsState();
 | 
				
			||||||
                        contentStream.addRect(0, 0, subPageWidth, subPageHeight);
 | 
					                        contentStream.addRect(0, 0, subPageWidth, subPageHeight);
 | 
				
			||||||
@ -137,4 +137,7 @@ public class SplitPdfBySectionsController {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return splitDocuments;
 | 
					        return splitDocuments;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,8 @@ import java.io.InputStream;
 | 
				
			|||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.zip.ZipEntry;
 | 
					import java.util.zip.ZipEntry;
 | 
				
			||||||
import java.util.zip.ZipOutputStream;
 | 
					import java.util.zip.ZipOutputStream;
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
@ -80,13 +82,31 @@ public class PipelineController {
 | 
				
			|||||||
                return null;
 | 
					                return null;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
         // Create a ByteArrayOutputStream to hold the zip
 | 
					         // Create a ByteArrayOutputStream to hold the zip
 | 
				
			||||||
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
					         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
				
			||||||
         ZipOutputStream zipOut = new ZipOutputStream(baos);
 | 
					         ZipOutputStream zipOut = new ZipOutputStream(baos);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					         // A map to keep track of filenames and their counts
 | 
				
			||||||
 | 
					         Map<String, Integer> filenameCount = new HashMap<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
         // Loop through each file and add it to the zip
 | 
					         // Loop through each file and add it to the zip
 | 
				
			||||||
         for (Resource file : outputFiles) {
 | 
					         for (Resource file : outputFiles) {
 | 
				
			||||||
                ZipEntry zipEntry = new ZipEntry(file.getFilename());
 | 
					             String originalFilename = file.getFilename();
 | 
				
			||||||
 | 
					             String filename = originalFilename;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					             // Check if the filename already exists, and modify it if necessary
 | 
				
			||||||
 | 
					             if (filenameCount.containsKey(originalFilename)) {
 | 
				
			||||||
 | 
					                 int count = filenameCount.get(originalFilename);
 | 
				
			||||||
 | 
					                 String baseName = originalFilename.replaceAll("\\.[^.]*$", "");
 | 
				
			||||||
 | 
					                 String extension = originalFilename.replaceAll("^.*\\.", "");
 | 
				
			||||||
 | 
					                 filename = baseName + "(" + count + ")." + extension;
 | 
				
			||||||
 | 
					                 filenameCount.put(originalFilename, count + 1);
 | 
				
			||||||
 | 
					             } else {
 | 
				
			||||||
 | 
					                 filenameCount.put(originalFilename, 1);
 | 
				
			||||||
 | 
					             }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					             ZipEntry zipEntry = new ZipEntry(filename);
 | 
				
			||||||
             zipOut.putNextEntry(zipEntry);
 | 
					             zipOut.putNextEntry(zipEntry);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
             // Read the file into a byte array
 | 
					             // Read the file into a byte array
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,8 @@ import java.io.ByteArrayOutputStream;
 | 
				
			|||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.io.PrintStream;
 | 
					import java.io.PrintStream;
 | 
				
			||||||
 | 
					import java.net.URLDecoder;
 | 
				
			||||||
 | 
					import java.nio.charset.StandardCharsets;
 | 
				
			||||||
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;
 | 
				
			||||||
@ -217,11 +219,12 @@ public class PipelineProcessor {
 | 
				
			|||||||
            throws IOException {
 | 
					            throws IOException {
 | 
				
			||||||
        // Define filename
 | 
					        // Define filename
 | 
				
			||||||
        String newFilename;
 | 
					        String newFilename;
 | 
				
			||||||
        if ("auto-rename".equals(operation)) {
 | 
					        if (operation.contains("auto-rename")) {
 | 
				
			||||||
            // If the operation is "auto-rename", generate a new filename.
 | 
					            // If the operation is "auto-rename", generate a new filename.
 | 
				
			||||||
            // This is a simple example of generating a filename using current timestamp.
 | 
					            // This is a simple example of generating a filename using current timestamp.
 | 
				
			||||||
            // Modify as per your needs.
 | 
					            // Modify as per your needs.
 | 
				
			||||||
            newFilename = "file_" + System.currentTimeMillis();
 | 
					        	
 | 
				
			||||||
 | 
					            newFilename = extractFilename(response);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            // Otherwise, keep the original filename.
 | 
					            // Otherwise, keep the original filename.
 | 
				
			||||||
            newFilename = fileName;
 | 
					            newFilename = fileName;
 | 
				
			||||||
@ -245,6 +248,29 @@ public class PipelineProcessor {
 | 
				
			|||||||
        return newOutputFiles;
 | 
					        return newOutputFiles;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public String extractFilename(ResponseEntity<byte[]> response) {
 | 
				
			||||||
 | 
					    String filename = "default-filename.ext"; // Default filename if not found
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    HttpHeaders headers = response.getHeaders();
 | 
				
			||||||
 | 
					    String contentDisposition = headers.getFirst(HttpHeaders.CONTENT_DISPOSITION);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (contentDisposition != null && !contentDisposition.isEmpty()) {
 | 
				
			||||||
 | 
					        String[] parts = contentDisposition.split(";");
 | 
				
			||||||
 | 
					        for (String part : parts) {
 | 
				
			||||||
 | 
					            if (part.trim().startsWith("filename")) {
 | 
				
			||||||
 | 
					                // Extracts filename and removes quotes if present
 | 
				
			||||||
 | 
					                filename = part.split("=")[1].trim().replace("\"", "");
 | 
				
			||||||
 | 
					                filename = URLDecoder.decode(filename, StandardCharsets.UTF_8);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return filename;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    List<Resource> generateInputFiles(File[] files) throws Exception {
 | 
					    List<Resource> generateInputFiles(File[] files) throws Exception {
 | 
				
			||||||
        if (files == null || files.length == 0) {
 | 
					        if (files == null || files.length == 0) {
 | 
				
			||||||
            logger.info("No files");
 | 
					            logger.info("No files");
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user