2023-07-09 00:05:33 +01:00
|
|
|
package stirling.software.SPDF.controller.api;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2023-09-03 01:24:02 +01:00
|
|
|
import org.apache.pdfbox.multipdf.LayerUtility;
|
2023-09-02 20:32:44 +01:00
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
import org.apache.pdfbox.pdmodel.PDPage;
|
2023-09-03 01:24:02 +01:00
|
|
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
2024-01-12 23:15:27 +00:00
|
|
|
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
|
2023-09-02 20:32:44 +01:00
|
|
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
2023-09-03 01:24:02 +01:00
|
|
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
2023-07-09 00:05:33 +01:00
|
|
|
import org.springframework.http.ResponseEntity;
|
2023-09-09 00:25:27 +01:00
|
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
2023-07-09 00:05:33 +01:00
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
2023-09-11 23:19:50 +01:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2023-07-09 00:05:33 +01:00
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2025-04-25 15:35:12 +02:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
2023-09-09 00:25:27 +01:00
|
|
|
import stirling.software.SPDF.model.api.general.CropPdfForm;
|
2025-03-12 13:13:44 +01:00
|
|
|
import stirling.software.SPDF.service.CustomPDFDocumentFactory;
|
2023-07-09 00:05:33 +01:00
|
|
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
|
|
|
|
|
|
|
@RestController
|
2023-09-11 23:19:50 +01:00
|
|
|
@RequestMapping("/api/v1/general")
|
2023-07-09 00:05:33 +01:00
|
|
|
@Tag(name = "General", description = "General APIs")
|
2025-04-25 15:35:12 +02:00
|
|
|
@RequiredArgsConstructor
|
2023-07-09 00:05:33 +01:00
|
|
|
public class CropController {
|
|
|
|
|
2025-03-12 13:13:44 +01:00
|
|
|
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
2024-09-14 16:29:39 +01:00
|
|
|
|
2023-07-09 00:05:33 +01:00
|
|
|
@PostMapping(value = "/crop", consumes = "multipart/form-data")
|
|
|
|
@Operation(
|
|
|
|
summary = "Crops a PDF document",
|
|
|
|
description =
|
2025-03-12 13:13:44 +01:00
|
|
|
"This operation takes an input PDF file and crops it according to the given"
|
|
|
|
+ " coordinates. Input:PDF Output:PDF Type:SISO")
|
Improve Type Safety and OpenAPI Schema for PDF API Controllers and Models (#3470)
# 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.
2025-05-16 13:23:01 +02:00
|
|
|
public ResponseEntity<byte[]> cropPdf(@ModelAttribute CropPdfForm request) throws IOException {
|
|
|
|
PDDocument sourceDocument = pdfDocumentFactory.load(request);
|
2023-09-02 20:32:44 +01:00
|
|
|
|
2024-09-14 16:29:39 +01:00
|
|
|
PDDocument newDocument =
|
|
|
|
pdfDocumentFactory.createNewDocumentBasedOnOldDocument(sourceDocument);
|
2023-09-02 20:32:44 +01:00
|
|
|
|
2023-09-09 00:25:27 +01:00
|
|
|
int totalPages = sourceDocument.getNumberOfPages();
|
2023-09-02 20:32:44 +01:00
|
|
|
|
|
|
|
LayerUtility layerUtility = new LayerUtility(newDocument);
|
|
|
|
|
|
|
|
for (int i = 0; i < totalPages; i++) {
|
|
|
|
PDPage sourcePage = sourceDocument.getPage(i);
|
|
|
|
|
|
|
|
// Create a new page with the size of the source page
|
|
|
|
PDPage newPage = new PDPage(sourcePage.getMediaBox());
|
|
|
|
newDocument.addPage(newPage);
|
2024-01-12 23:15:27 +00:00
|
|
|
PDPageContentStream contentStream =
|
|
|
|
new PDPageContentStream(newDocument, newPage, AppendMode.OVERWRITE, true, true);
|
2023-09-02 20:32:44 +01:00
|
|
|
|
|
|
|
// Import the source page as a form XObject
|
|
|
|
PDFormXObject formXObject = layerUtility.importPageAsForm(sourceDocument, i);
|
|
|
|
|
|
|
|
contentStream.saveGraphicsState();
|
|
|
|
|
|
|
|
// Define the crop area
|
Improve Type Safety and OpenAPI Schema for PDF API Controllers and Models (#3470)
# 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.
2025-05-16 13:23:01 +02:00
|
|
|
contentStream.addRect(
|
|
|
|
request.getX(), request.getY(), request.getWidth(), request.getHeight());
|
2023-09-02 20:32:44 +01:00
|
|
|
contentStream.clip();
|
|
|
|
|
|
|
|
// Draw the entire formXObject
|
|
|
|
contentStream.drawForm(formXObject);
|
|
|
|
|
|
|
|
contentStream.restoreGraphicsState();
|
|
|
|
|
|
|
|
contentStream.close();
|
|
|
|
|
|
|
|
// Now, set the new page's media box to the cropped size
|
2023-09-09 00:25:27 +01:00
|
|
|
newPage.setMediaBox(
|
Improve Type Safety and OpenAPI Schema for PDF API Controllers and Models (#3470)
# 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.
2025-05-16 13:23:01 +02:00
|
|
|
new PDRectangle(
|
|
|
|
request.getX(),
|
|
|
|
request.getY(),
|
|
|
|
request.getWidth(),
|
|
|
|
request.getHeight()));
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
2023-09-02 20:32:44 +01:00
|
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
newDocument.save(baos);
|
2023-09-09 00:25:27 +01:00
|
|
|
newDocument.close();
|
|
|
|
sourceDocument.close();
|
2023-07-09 00:05:33 +01:00
|
|
|
|
2023-09-02 20:32:44 +01:00
|
|
|
byte[] pdfContent = baos.toByteArray();
|
2023-09-09 00:25:27 +01:00
|
|
|
return WebResponseUtils.bytesToWebResponse(
|
2023-12-30 19:11:27 +00:00
|
|
|
pdfContent,
|
Improve Type Safety and OpenAPI Schema for PDF API Controllers and Models (#3470)
# 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.
2025-05-16 13:23:01 +02:00
|
|
|
request.getFileInput().getOriginalFilename().replaceFirst("[.][^.]+$", "")
|
2023-09-09 00:25:27 +01:00
|
|
|
+ "_cropped.pdf");
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
2023-07-09 00:05:33 +01:00
|
|
|
}
|