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;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.RequestPart;
|
|
|
|
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;
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
2023-05-31 21:15:48 +02:00
|
|
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
2023-04-29 00:18:10 +02:00
|
|
|
|
|
|
|
@RestController
|
|
|
|
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-21 00:49: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"
|
2023-05-13 11:32:47 +02:00
|
|
|
)
|
|
|
|
public ResponseEntity<byte[]> rotatePDF(
|
|
|
|
@RequestPart(required = true, value = "fileInput")
|
|
|
|
@Parameter(description = "The PDF file to be rotated", required = true)
|
|
|
|
MultipartFile pdfFile,
|
|
|
|
@RequestParam("angle")
|
|
|
|
@Parameter(description = "The angle by which to rotate the PDF file. This should be a multiple of 90.", example = "90", required = true)
|
|
|
|
Integer angle) throws IOException {
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|