mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-14 18:44:05 +02:00
# Description of Changes - **What was changed** - Updated controller methods to use strongly‐typed primitives (`int`, `long`, `boolean`) instead of `String` for numeric and boolean parameters, eliminating calls to `Integer.parseInt`/`Long.parseLong` and improving null‐safety (`Boolean.TRUE.equals(...)`). - Enhanced all API request model classes with richer Swagger/OpenAPI annotations: added `requiredMode`, `defaultValue`, `allowableValues`, `format`, `pattern`, and tightened schema descriptions for all fields. - Refactored HTML form templates for “Remove Blank Pages” to include `min`, `max`, and `step` attributes on numeric inputs, matching the updated validation rules. - **Why the change was made** - **Type safety & robustness**: Shifting from `String` to native types prevents runtime parsing errors, simplifies controller logic, and makes default values explicit. - **Better API documentation & validation**: Enriching the Swagger annotations ensures generated docs accurately reflect required fields, default values, and permitted ranges, which improves client code generation and developer experience. - **Consistency across codebase**: Aligning all request models and controllers enforces a uniform coding style and reduces bugs. #3406 --- ## Checklist ### General - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [x] I have performed a self-review of my own code - [x] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details.
204 lines
8.9 KiB
Java
204 lines
8.9 KiB
Java
package stirling.software.SPDF.controller.api;
|
|
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
|
|
import org.apache.pdfbox.cos.COSName;
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
|
import org.apache.pdfbox.pdmodel.PDPage;
|
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
import org.apache.pdfbox.pdmodel.encryption.PDEncryption;
|
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
|
|
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import stirling.software.SPDF.model.api.PDFFile;
|
|
import stirling.software.SPDF.service.CustomPDFDocumentFactory;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/v1/analysis")
|
|
@Tag(name = "Analysis", description = "Analysis APIs")
|
|
@RequiredArgsConstructor
|
|
public class AnalysisController {
|
|
|
|
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
|
|
|
@PostMapping(value = "/page-count", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Get PDF page count",
|
|
description = "Returns total number of pages in PDF. Input:PDF Output:JSON Type:SISO")
|
|
public Map<String, Integer> getPageCount(@ModelAttribute PDFFile file) throws IOException {
|
|
try (PDDocument document = pdfDocumentFactory.load(file.getFileInput())) {
|
|
return Map.of("pageCount", document.getNumberOfPages());
|
|
}
|
|
}
|
|
|
|
@PostMapping(value = "/basic-info", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Get basic PDF information",
|
|
description = "Returns page count, version, file size. Input:PDF Output:JSON Type:SISO")
|
|
public Map<String, Object> getBasicInfo(@ModelAttribute PDFFile file) throws IOException {
|
|
try (PDDocument document = pdfDocumentFactory.load(file.getFileInput())) {
|
|
Map<String, Object> info = new HashMap<>();
|
|
info.put("pageCount", document.getNumberOfPages());
|
|
info.put("pdfVersion", document.getVersion());
|
|
info.put("fileSize", file.getFileInput().getSize());
|
|
return info;
|
|
}
|
|
}
|
|
|
|
@PostMapping(value = "/document-properties", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Get PDF document properties",
|
|
description = "Returns title, author, subject, etc. Input:PDF Output:JSON Type:SISO")
|
|
public Map<String, String> getDocumentProperties(@ModelAttribute PDFFile file)
|
|
throws IOException {
|
|
// Load the document in read-only mode to prevent modifications and ensure the integrity of
|
|
// the original file.
|
|
try (PDDocument document = pdfDocumentFactory.load(file.getFileInput(), true)) {
|
|
PDDocumentInformation info = document.getDocumentInformation();
|
|
Map<String, String> properties = new HashMap<>();
|
|
properties.put("title", info.getTitle());
|
|
properties.put("author", info.getAuthor());
|
|
properties.put("subject", info.getSubject());
|
|
properties.put("keywords", info.getKeywords());
|
|
properties.put("creator", info.getCreator());
|
|
properties.put("producer", info.getProducer());
|
|
properties.put("creationDate", info.getCreationDate().toString());
|
|
properties.put("modificationDate", info.getModificationDate().toString());
|
|
return properties;
|
|
}
|
|
}
|
|
|
|
@PostMapping(value = "/page-dimensions", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Get page dimensions for all pages",
|
|
description = "Returns width and height of each page. Input:PDF Output:JSON Type:SISO")
|
|
public List<Map<String, Float>> getPageDimensions(@ModelAttribute PDFFile file)
|
|
throws IOException {
|
|
try (PDDocument document = pdfDocumentFactory.load(file.getFileInput())) {
|
|
List<Map<String, Float>> dimensions = new ArrayList<>();
|
|
PDPageTree pages = document.getPages();
|
|
|
|
for (PDPage page : pages) {
|
|
Map<String, Float> pageDim = new HashMap<>();
|
|
pageDim.put("width", page.getBBox().getWidth());
|
|
pageDim.put("height", page.getBBox().getHeight());
|
|
dimensions.add(pageDim);
|
|
}
|
|
return dimensions;
|
|
}
|
|
}
|
|
|
|
@PostMapping(value = "/form-fields", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Get form field information",
|
|
description =
|
|
"Returns count and details of form fields. Input:PDF Output:JSON Type:SISO")
|
|
public Map<String, Object> getFormFields(@ModelAttribute PDFFile file) throws IOException {
|
|
try (PDDocument document = pdfDocumentFactory.load(file.getFileInput())) {
|
|
Map<String, Object> formInfo = new HashMap<>();
|
|
PDAcroForm form = document.getDocumentCatalog().getAcroForm();
|
|
|
|
if (form != null) {
|
|
formInfo.put("fieldCount", form.getFields().size());
|
|
formInfo.put("hasXFA", form.hasXFA());
|
|
formInfo.put("isSignaturesExist", form.isSignaturesExist());
|
|
} else {
|
|
formInfo.put("fieldCount", 0);
|
|
formInfo.put("hasXFA", false);
|
|
formInfo.put("isSignaturesExist", false);
|
|
}
|
|
return formInfo;
|
|
}
|
|
}
|
|
|
|
@PostMapping(value = "/annotation-info", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Get annotation information",
|
|
description = "Returns count and types of annotations. Input:PDF Output:JSON Type:SISO")
|
|
public Map<String, Object> getAnnotationInfo(@ModelAttribute PDFFile file) throws IOException {
|
|
try (PDDocument document = pdfDocumentFactory.load(file.getFileInput())) {
|
|
Map<String, Object> annotInfo = new HashMap<>();
|
|
int totalAnnotations = 0;
|
|
Map<String, Integer> annotationTypes = new HashMap<>();
|
|
|
|
for (PDPage page : document.getPages()) {
|
|
for (PDAnnotation annot : page.getAnnotations()) {
|
|
totalAnnotations++;
|
|
String subType = annot.getSubtype();
|
|
annotationTypes.merge(subType, 1, Integer::sum);
|
|
}
|
|
}
|
|
|
|
annotInfo.put("totalCount", totalAnnotations);
|
|
annotInfo.put("typeBreakdown", annotationTypes);
|
|
return annotInfo;
|
|
}
|
|
}
|
|
|
|
@PostMapping(value = "/font-info", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Get font information",
|
|
description =
|
|
"Returns list of fonts used in the document. Input:PDF Output:JSON Type:SISO")
|
|
public Map<String, Object> getFontInfo(@ModelAttribute PDFFile file) throws IOException {
|
|
try (PDDocument document = pdfDocumentFactory.load(file.getFileInput())) {
|
|
Map<String, Object> fontInfo = new HashMap<>();
|
|
Set<String> fontNames = new HashSet<>();
|
|
|
|
for (PDPage page : document.getPages()) {
|
|
for (COSName font : page.getResources().getFontNames()) {
|
|
fontNames.add(font.getName());
|
|
}
|
|
}
|
|
|
|
fontInfo.put("fontCount", fontNames.size());
|
|
fontInfo.put("fonts", fontNames);
|
|
return fontInfo;
|
|
}
|
|
}
|
|
|
|
@PostMapping(value = "/security-info", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Get security information",
|
|
description =
|
|
"Returns encryption and permission details. Input:PDF Output:JSON Type:SISO")
|
|
public Map<String, Object> getSecurityInfo(@ModelAttribute PDFFile file) throws IOException {
|
|
try (PDDocument document = pdfDocumentFactory.load(file.getFileInput())) {
|
|
Map<String, Object> securityInfo = new HashMap<>();
|
|
PDEncryption encryption = document.getEncryption();
|
|
|
|
if (encryption != null) {
|
|
securityInfo.put("isEncrypted", true);
|
|
securityInfo.put("keyLength", encryption.getLength());
|
|
|
|
// Get permissions
|
|
Map<String, Boolean> permissions = new HashMap<>();
|
|
permissions.put(
|
|
"preventPrinting", !document.getCurrentAccessPermission().canPrint());
|
|
permissions.put(
|
|
"preventModify", !document.getCurrentAccessPermission().canModify());
|
|
permissions.put(
|
|
"preventExtractContent",
|
|
!document.getCurrentAccessPermission().canExtractContent());
|
|
permissions.put(
|
|
"preventModifyAnnotations",
|
|
!document.getCurrentAccessPermission().canModifyAnnotations());
|
|
|
|
securityInfo.put("permissions", permissions);
|
|
} else {
|
|
securityInfo.put("isEncrypted", false);
|
|
}
|
|
|
|
return securityInfo;
|
|
}
|
|
}
|
|
}
|