2023-04-29 00:18:10 +02:00
|
|
|
package stirling.software.SPDF.controller.api;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2023-09-09 01:25:27 +02:00
|
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
2023-04-29 00:18:10 +02:00
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
2023-09-12 00:19:50 +02:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2023-04-29 00:18:10 +02:00
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
2023-05-13 11:32:47 +02:00
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
2023-06-25 10:16:32 +02:00
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2023-09-09 01:25:27 +02:00
|
|
|
import stirling.software.SPDF.model.api.general.RotatePDFRequest;
|
2023-05-31 21:15:48 +02:00
|
|
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
2023-04-29 00:18:10 +02:00
|
|
|
|
|
|
|
@RestController
|
2023-09-12 00:19:50 +02:00
|
|
|
@RequestMapping("/api/v1/general")
|
2023-06-25 10:16:32 +02:00
|
|
|
@Tag(name = "General", description = "General APIs")
|
2023-04-29 00:18:10 +02:00
|
|
|
public class RotationController {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RotationController.class);
|
|
|
|
|
|
|
|
@PostMapping(consumes = "multipart/form-data", value = "/rotate-pdf")
|
2023-05-13 11:32:47 +02:00
|
|
|
@Operation(
|
|
|
|
summary = "Rotate a PDF file",
|
2023-06-24 00:29:53 +02:00
|
|
|
description = "This endpoint rotates a given PDF file by a specified angle. The angle must be a multiple of 90. Input:PDF Output:PDF Type:SISO"
|
2023-05-13 11:32:47 +02:00
|
|
|
)
|
|
|
|
public ResponseEntity<byte[]> rotatePDF(
|
2023-09-09 01:25:27 +02:00
|
|
|
@ModelAttribute RotatePDFRequest request) throws IOException {
|
|
|
|
MultipartFile pdfFile = request.getFileInput();
|
|
|
|
Integer angle = request.getAngle();
|
2023-04-29 00:18:10 +02:00
|
|
|
// Load the PDF document
|
|
|
|
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
|
|
|
|
|
|
|
// Get the list of pages in the document
|
|
|
|
PDPageTree pages = document.getPages();
|
|
|
|
|
|
|
|
for (PDPage page : pages) {
|
|
|
|
page.setRotation(page.getRotation() + angle);
|
|
|
|
}
|
|
|
|
|
2023-05-31 21:15:48 +02:00
|
|
|
return WebResponseUtils.pdfDocToWebResponse(document, pdfFile.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_rotated.pdf");
|
2023-04-29 00:18:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|