updates
This commit is contained in:
@@ -26,11 +26,18 @@ public class InitialSecuritySetup {
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
if (!userService.hasUsers()) {
|
||||
String initialUsername = "admin";
|
||||
String initialPassword = "stirling";
|
||||
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId(), true);
|
||||
|
||||
|
||||
String initialUsername = applicationProperties.getSecurity().getInitialLogin().getUsername();
|
||||
String initialPassword = applicationProperties.getSecurity().getInitialLogin().getPassword();
|
||||
if (initialUsername != null && initialPassword != null) {
|
||||
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId());
|
||||
} else {
|
||||
initialUsername = "admin";
|
||||
initialPassword = "stirling";
|
||||
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId(), true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package stirling.software.SPDF.controller.api;
|
||||
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -42,7 +43,8 @@ public class MultiPageLayoutController {
|
||||
|
||||
int pagesPerSheet = request.getPagesPerSheet();
|
||||
MultipartFile file = request.getFileInput();
|
||||
|
||||
boolean addBorder = request.isAddBorder();
|
||||
|
||||
if (pagesPerSheet != 2 && pagesPerSheet != 3 && pagesPerSheet != (int) Math.sqrt(pagesPerSheet) * Math.sqrt(pagesPerSheet)) {
|
||||
throw new IllegalArgumentException("pagesPerSheet must be 2, 3 or a perfect square");
|
||||
}
|
||||
@@ -62,6 +64,10 @@ public class MultiPageLayoutController {
|
||||
PDPageContentStream contentStream = new PDPageContentStream(newDocument, newPage, PDPageContentStream.AppendMode.APPEND, true, true);
|
||||
LayerUtility layerUtility = new LayerUtility(newDocument);
|
||||
|
||||
float borderThickness = 1.5f; // Specify border thickness as required
|
||||
contentStream.setLineWidth(borderThickness);
|
||||
contentStream.setStrokingColor(Color.BLACK);
|
||||
|
||||
for (int i = 0; i < totalPages; i++) {
|
||||
if (i != 0 && i % pagesPerSheet == 0) {
|
||||
// Close the current content stream and create a new page and content stream
|
||||
@@ -92,6 +98,14 @@ public class MultiPageLayoutController {
|
||||
contentStream.drawForm(formXObject);
|
||||
|
||||
contentStream.restoreGraphicsState();
|
||||
|
||||
if(addBorder) {
|
||||
// Draw border around each page
|
||||
float borderX = colIndex * cellWidth;
|
||||
float borderY = newPage.getMediaBox().getHeight() - (rowIndex + 1) * cellHeight;
|
||||
contentStream.addRect(borderX, borderY, cellWidth, cellHeight);
|
||||
contentStream.stroke();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package stirling.software.SPDF.controller.api.misc;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -58,7 +61,8 @@ public class ExtractImagesController {
|
||||
|
||||
int imageIndex = 1;
|
||||
String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", "");
|
||||
int pageNum = 1;
|
||||
int pageNum = 0;
|
||||
Set<Integer> processedImages = new HashSet<>();
|
||||
// Iterate over each page
|
||||
for (PDPage page : document.getPages()) {
|
||||
++pageNum;
|
||||
@@ -66,7 +70,12 @@ public class ExtractImagesController {
|
||||
for (COSName name : page.getResources().getXObjectNames()) {
|
||||
if (page.getResources().isImageXObject(name)) {
|
||||
PDImageXObject image = (PDImageXObject) page.getResources().getXObject(name);
|
||||
|
||||
int imageHash = image.hashCode();
|
||||
if(processedImages.contains(imageHash)) {
|
||||
continue; // Skip already processed images
|
||||
}
|
||||
processedImages.add(imageHash);
|
||||
|
||||
// Convert image to desired format
|
||||
RenderedImage renderedImage = image.getImage();
|
||||
BufferedImage bufferedImage = null;
|
||||
|
||||
@@ -386,12 +386,16 @@ public class GetInfoOnPDF {
|
||||
float width = mediaBox.getWidth();
|
||||
float height = mediaBox.getHeight();
|
||||
|
||||
pageInfo.put("Width", width);
|
||||
pageInfo.put("Height", height);
|
||||
ObjectNode sizeInfo = objectMapper.createObjectNode();
|
||||
|
||||
getDimensionInfo(sizeInfo, width, height);
|
||||
|
||||
sizeInfo.put("Standard Page", getPageSize(width, height));
|
||||
pageInfo.set("Size", sizeInfo);
|
||||
|
||||
pageInfo.put("Rotation", page.getRotation());
|
||||
|
||||
pageInfo.put("Page Orientation", getPageOrientation(width, height));
|
||||
pageInfo.put("Standard Size", getPageSize(width, height));
|
||||
|
||||
|
||||
// Boxes
|
||||
pageInfo.put("MediaBox", mediaBox.toString());
|
||||
@@ -620,7 +624,7 @@ public class GetInfoOnPDF {
|
||||
|
||||
|
||||
|
||||
pageInfoParent.set("Page " + pageNum, pageInfo);
|
||||
pageInfoParent.set("Page " + (pageNum+1), pageInfo);
|
||||
}
|
||||
|
||||
|
||||
@@ -670,28 +674,52 @@ public class GetInfoOnPDF {
|
||||
return "Square";
|
||||
}
|
||||
}
|
||||
public String getPageSize(double width, double height) {
|
||||
// Common aspect ratios used for standard paper sizes
|
||||
double[] aspectRatios = {4.0 / 3.0, 3.0 / 2.0, Math.sqrt(2.0), 16.0 / 9.0};
|
||||
public String getPageSize(float width, float height) {
|
||||
// Define standard page sizes
|
||||
Map<String, PDRectangle> standardSizes = new HashMap<>();
|
||||
standardSizes.put("Letter", PDRectangle.LETTER);
|
||||
standardSizes.put("LEGAL", PDRectangle.LEGAL);
|
||||
standardSizes.put("A0", PDRectangle.A0);
|
||||
standardSizes.put("A1", PDRectangle.A1);
|
||||
standardSizes.put("A2", PDRectangle.A2);
|
||||
standardSizes.put("A3", PDRectangle.A3);
|
||||
standardSizes.put("A4", PDRectangle.A4);
|
||||
standardSizes.put("A5", PDRectangle.A5);
|
||||
standardSizes.put("A6", PDRectangle.A6);
|
||||
|
||||
// Check if the page matches any common aspect ratio
|
||||
for (double aspectRatio : aspectRatios) {
|
||||
if (isCloseToAspectRatio(width, height, aspectRatio)) {
|
||||
return "Standard";
|
||||
for (Map.Entry<String, PDRectangle> entry : standardSizes.entrySet()) {
|
||||
PDRectangle size = entry.getValue();
|
||||
if (isCloseToSize(width, height, size.getWidth(), size.getHeight())) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
|
||||
// If not a standard aspect ratio, consider it as a custom size
|
||||
return "Custom";
|
||||
}
|
||||
private boolean isCloseToAspectRatio(double width, double height, double aspectRatio) {
|
||||
// Calculate the aspect ratio of the page
|
||||
double pageAspectRatio = width / height;
|
||||
|
||||
// Compare the page aspect ratio with the common aspect ratio within a threshold
|
||||
return Math.abs(pageAspectRatio - aspectRatio) <= 0.05;
|
||||
private boolean isCloseToSize(float width, float height, float standardWidth, float standardHeight) {
|
||||
float tolerance = 1.0f; // You can adjust the tolerance as needed
|
||||
return Math.abs(width - standardWidth) <= tolerance && Math.abs(height - standardHeight) <= tolerance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ObjectNode getDimensionInfo(ObjectNode dimensionInfo, float width, float height) {
|
||||
float ppi = 72; // Points Per Inch
|
||||
|
||||
float widthInInches = width / ppi;
|
||||
float heightInInches = height / ppi;
|
||||
|
||||
float widthInCm = widthInInches * 2.54f;
|
||||
float heightInCm = heightInInches * 2.54f;
|
||||
|
||||
dimensionInfo.put("Width (px)", String.format("%.2f", width));
|
||||
dimensionInfo.put("Height (px)", String.format("%.2f", height));
|
||||
dimensionInfo.put("Width (in)", String.format("%.2f", widthInInches));
|
||||
dimensionInfo.put("Height (in)", String.format("%.2f", heightInInches));
|
||||
dimensionInfo.put("Width (cm)", String.format("%.2f", widthInCm));
|
||||
dimensionInfo.put("Height (cm)", String.format("%.2f", heightInCm));
|
||||
return dimensionInfo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean checkForStandard(PDDocument document, String standardKeyword) {
|
||||
|
||||
@@ -105,7 +105,16 @@ public class ApplicationProperties {
|
||||
public static class Security {
|
||||
private Boolean enableLogin;
|
||||
private Boolean csrfDisabled;
|
||||
private InitialLogin initialLogin;
|
||||
|
||||
public InitialLogin getInitialLogin() {
|
||||
return initialLogin != null ? initialLogin : new InitialLogin();
|
||||
}
|
||||
|
||||
public void setInitialLogin(InitialLogin initialLogin) {
|
||||
this.initialLogin = initialLogin;
|
||||
}
|
||||
|
||||
public Boolean getEnableLogin() {
|
||||
return enableLogin;
|
||||
}
|
||||
@@ -125,9 +134,39 @@ public class ApplicationProperties {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Security [enableLogin=" + enableLogin + ", csrfDisabled="
|
||||
return "Security [enableLogin=" + enableLogin + ", initialLogin=" + initialLogin + ", csrfDisabled="
|
||||
+ csrfDisabled + "]";
|
||||
}
|
||||
|
||||
public static class InitialLogin {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "InitialLogin [username=" + username + ", password=" + (password != null && !password.isEmpty() ? "MASKED" : "NULL") + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class System {
|
||||
|
||||
@@ -12,4 +12,7 @@ public class MergeMultiplePagesRequest extends PDFFile {
|
||||
@Schema(description = "The number of pages to fit onto a single sheet in the output PDF.",
|
||||
type = "integer", allowableValues = {"2", "3", "4", "9", "16"})
|
||||
private int pagesPerSheet;
|
||||
|
||||
@Schema(description = "Boolean for if you wish to add border around the pages")
|
||||
private boolean addBorder;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user