2024-08-08 20:38:36 +01:00
|
|
|
package stirling.software.SPDF.controller.api;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
|
|
|
import stirling.software.SPDF.model.api.PDFFile;
|
2024-09-14 16:29:39 +01:00
|
|
|
import stirling.software.SPDF.service.CustomPDDocumentFactory;
|
2024-08-08 20:38:36 +01:00
|
|
|
import stirling.software.SPDF.service.PdfImageRemovalService;
|
|
|
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
|
|
|
|
|
|
|
/**
|
2024-08-08 21:13:59 +01:00
|
|
|
* Controller class for handling PDF image removal requests. Provides an endpoint to remove images
|
|
|
|
* from a PDF file to reduce its size.
|
2024-08-08 20:38:36 +01:00
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/api/v1/general")
|
|
|
|
public class PdfImageRemovalController {
|
|
|
|
|
|
|
|
// Service for removing images from PDFs
|
2024-09-14 16:29:39 +01:00
|
|
|
private final PdfImageRemovalService pdfImageRemovalService;
|
|
|
|
|
|
|
|
private final CustomPDDocumentFactory pdfDocumentFactory;
|
2024-08-08 20:38:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for dependency injection of PdfImageRemovalService.
|
|
|
|
*
|
|
|
|
* @param pdfImageRemovalService The service used for removing images from PDFs.
|
|
|
|
*/
|
2024-09-14 16:29:39 +01:00
|
|
|
@Autowired
|
|
|
|
public PdfImageRemovalController(
|
|
|
|
PdfImageRemovalService pdfImageRemovalService,
|
|
|
|
CustomPDDocumentFactory pdfDocumentFactory) {
|
2024-08-08 20:38:36 +01:00
|
|
|
this.pdfImageRemovalService = pdfImageRemovalService;
|
2024-09-14 16:29:39 +01:00
|
|
|
this.pdfDocumentFactory = pdfDocumentFactory;
|
2024-08-08 20:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint to remove images from a PDF file.
|
|
|
|
*
|
2024-08-08 21:13:59 +01:00
|
|
|
* <p>This method processes the uploaded PDF file, removes all images, and returns the modified
|
|
|
|
* PDF file with a new name indicating that images were removed.
|
2024-08-08 20:38:36 +01:00
|
|
|
*
|
|
|
|
* @param file The PDF file with images to be removed.
|
2024-08-08 21:13:59 +01:00
|
|
|
* @return ResponseEntity containing the modified PDF file as byte array with appropriate
|
|
|
|
* content type and filename.
|
2024-08-08 20:38:36 +01:00
|
|
|
* @throws IOException If an error occurs while processing the PDF file.
|
|
|
|
*/
|
|
|
|
@PostMapping(consumes = "multipart/form-data", value = "/remove-image-pdf")
|
|
|
|
@Operation(
|
|
|
|
summary = "Remove images from file to reduce the file size.",
|
|
|
|
description =
|
|
|
|
"This endpoint remove images from file to reduce the file size.Input:PDF Output:PDF Type:MISO")
|
|
|
|
public ResponseEntity<byte[]> removeImages(@ModelAttribute PDFFile file) throws IOException {
|
2024-09-14 16:29:39 +01:00
|
|
|
// Load the PDF document
|
|
|
|
PDDocument document = pdfDocumentFactory.load(file);
|
2024-08-08 20:38:36 +01:00
|
|
|
|
|
|
|
// Remove images from the PDF document using the service
|
|
|
|
PDDocument modifiedDocument = pdfImageRemovalService.removeImagesFromPdf(document);
|
|
|
|
|
|
|
|
// Create a ByteArrayOutputStream to hold the modified PDF data
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
// Save the modified PDF document to the output stream
|
|
|
|
modifiedDocument.save(outputStream);
|
|
|
|
modifiedDocument.close();
|
|
|
|
|
|
|
|
// Generate a new filename for the modified PDF
|
|
|
|
String mergedFileName =
|
2024-09-14 16:29:39 +01:00
|
|
|
file.getFileInput().getOriginalFilename().replaceFirst("[.][^.]+$", "")
|
|
|
|
+ "_removed_images.pdf";
|
2024-08-08 20:38:36 +01:00
|
|
|
|
|
|
|
// Convert the byte array to a web response and return it
|
|
|
|
return WebResponseUtils.bytesToWebResponse(outputStream.toByteArray(), mergedFileName);
|
|
|
|
}
|
|
|
|
}
|