2023-04-28 23:18:10 +01:00
|
|
|
package stirling.software.SPDF.controller.api;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2024-01-12 23:15:27 +00:00
|
|
|
import org.apache.pdfbox.Loader;
|
2023-04-28 23:18:10 +01:00
|
|
|
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 00:25:27 +01:00
|
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
2023-04-28 23:18:10 +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-04-28 23:18:10 +01:00
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
2024-02-06 00:00:49 +00:00
|
|
|
import io.github.pixee.security.Filenames;
|
2023-05-13 10:32:47 +01:00
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
2023-06-25 09:16:32 +01:00
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-09-09 00:25:27 +01:00
|
|
|
import stirling.software.SPDF.model.api.general.RotatePDFRequest;
|
2023-05-31 20:15:48 +01:00
|
|
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
2023-04-28 23:18:10 +01:00
|
|
|
|
|
|
|
@RestController
|
2023-09-11 23:19:50 +01:00
|
|
|
@RequestMapping("/api/v1/general")
|
2023-06-25 09:16:32 +01:00
|
|
|
@Tag(name = "General", description = "General APIs")
|
2023-04-28 23:18:10 +01:00
|
|
|
public class RotationController {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RotationController.class);
|
|
|
|
|
|
|
|
@PostMapping(consumes = "multipart/form-data", value = "/rotate-pdf")
|
2023-05-13 10:32:47 +01:00
|
|
|
@Operation(
|
|
|
|
summary = "Rotate a PDF file",
|
2023-06-23 23:29:53 +01: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-09-09 00:25:27 +01:00
|
|
|
public ResponseEntity<byte[]> rotatePDF(@ModelAttribute RotatePDFRequest request)
|
|
|
|
throws IOException {
|
|
|
|
MultipartFile pdfFile = request.getFileInput();
|
|
|
|
Integer angle = request.getAngle();
|
2023-04-28 23:18:10 +01:00
|
|
|
// Load the PDF document
|
2024-01-12 23:15:27 +00:00
|
|
|
PDDocument document = Loader.loadPDF(pdfFile.getBytes());
|
2023-04-28 23:18:10 +01:00
|
|
|
|
|
|
|
// Get the list of pages in the document
|
|
|
|
PDPageTree pages = document.getPages();
|
|
|
|
|
|
|
|
for (PDPage page : pages) {
|
|
|
|
page.setRotation(page.getRotation() + angle);
|
|
|
|
}
|
|
|
|
|
2023-05-31 20:15:48 +01:00
|
|
|
return WebResponseUtils.pdfDocToWebResponse(
|
|
|
|
document,
|
2024-02-06 00:00:49 +00:00
|
|
|
Filenames.toSimpleFileName(pdfFile.getOriginalFilename())
|
|
|
|
.replaceFirst("[.][^.]+$", "")
|
|
|
|
+ "_rotated.pdf");
|
2023-04-28 23:18:10 +01:00
|
|
|
}
|
|
|
|
}
|