-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Support for adding and removing passwords - Support for watermarks - Dedicated page remover - Support for PDF permissions - Removed endpoint /home and replaced with / - Code cleanups - Fixed page titles
- Loading branch information
Showing
34 changed files
with
1,245 additions
and
596 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/stirling/software/SPDF/controller/MergeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package stirling.software.SPDF.controller; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
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.core.io.InputStreamResource; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Controller | ||
public class MergeController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(MergeController.class); | ||
|
||
@GetMapping("/merge-pdfs") | ||
public String hello(Model model) { | ||
model.addAttribute("currentPage", "merge-pdfs"); | ||
return "merge-pdfs"; | ||
} | ||
|
||
@PostMapping("/merge-pdfs") | ||
public ResponseEntity<InputStreamResource> mergePdfs(@RequestParam("fileInput") MultipartFile[] files) | ||
throws IOException { | ||
// Read the input PDF files into PDDocument objects | ||
List<PDDocument> documents = new ArrayList<>(); | ||
|
||
// Loop through the files array and read each file into a PDDocument | ||
for (MultipartFile file : files) { | ||
documents.add(PDDocument.load(file.getInputStream())); | ||
} | ||
|
||
PDDocument mergedDoc = mergeDocuments(documents); | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
mergedDoc.save(byteArrayOutputStream); | ||
mergedDoc.close(); | ||
|
||
// Create an InputStreamResource from the merged PDF | ||
InputStreamResource resource = new InputStreamResource( | ||
new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); | ||
|
||
// Return the merged PDF as a response | ||
return ResponseEntity.ok().contentType(MediaType.APPLICATION_PDF).body(resource); | ||
} | ||
|
||
private PDDocument mergeDocuments(List<PDDocument> documents) throws IOException { | ||
// Create a new empty document | ||
PDDocument mergedDoc = new PDDocument(); | ||
|
||
// Iterate over the list of documents and add their pages to the merged document | ||
for (PDDocument doc : documents) { | ||
// Get all pages from the current document | ||
PDPageTree pages = doc.getPages(); | ||
// Iterate over the pages and add them to the merged document | ||
for (PDPage page : pages) { | ||
mergedDoc.addPage(page); | ||
} | ||
} | ||
|
||
// Return the merged document | ||
return mergedDoc; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.