|
@@ -0,0 +1,103 @@ |
|
|
|
|
|
package com.ffii.lioner.modules.lioner.pdf.web; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
|
import java.nio.file.Path; |
|
|
|
|
|
import java.nio.file.Paths; |
|
|
|
|
|
import java.nio.file.StandardCopyOption; |
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.pdfbox.Loader; |
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument; |
|
|
|
|
|
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; |
|
|
|
|
|
import org.apache.pdfbox.pdmodel.interactive.form.PDField; |
|
|
|
|
|
import org.springframework.core.io.Resource; |
|
|
|
|
|
import org.springframework.core.io.UrlResource; |
|
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin; |
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RestController |
|
|
|
|
|
@RequestMapping("/pdf2") |
|
|
|
|
|
@CrossOrigin(origins = "", allowedHeaders = "") |
|
|
|
|
|
|
|
|
|
|
|
public class Pdf2Controller { |
|
|
|
|
|
|
|
|
|
|
|
private final String UPLOAD_DIR = "uploads/"; // Configure this path |
|
|
|
|
|
|
|
|
|
|
|
// Endpoint to download the blank AcroForm |
|
|
|
|
|
@GetMapping("/download/template") |
|
|
|
|
|
public ResponseEntity<Resource> downloadPdfTemplate() throws IOException { |
|
|
|
|
|
Path filePath = Paths.get("src/main/resources/template/pdf/template_form.pdf").normalize(); // Path to your blank AcroForm |
|
|
|
|
|
Resource resource = new UrlResource(filePath.toUri()); |
|
|
|
|
|
|
|
|
|
|
|
if (resource.exists()) { |
|
|
|
|
|
return ResponseEntity.ok() |
|
|
|
|
|
.contentType(MediaType.APPLICATION_PDF) |
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") |
|
|
|
|
|
.body(resource); |
|
|
|
|
|
} else { |
|
|
|
|
|
return ResponseEntity.notFound().build(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Endpoint to upload the edited PDF |
|
|
|
|
|
@PostMapping("/upload") |
|
|
|
|
|
public ResponseEntity<String> uploadEditedPdf(@RequestParam("file") MultipartFile file) { |
|
|
|
|
|
try { |
|
|
|
|
|
// Ensure upload directory exists |
|
|
|
|
|
Path uploadPath = Paths.get(UPLOAD_DIR).toAbsolutePath().normalize(); |
|
|
|
|
|
Files.createDirectories(uploadPath); |
|
|
|
|
|
|
|
|
|
|
|
String fileName = UUID.randomUUID().toString() + "-" + StringUtils.cleanPath(file.getOriginalFilename()); |
|
|
|
|
|
Path targetLocation = uploadPath.resolve(fileName); |
|
|
|
|
|
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING); |
|
|
|
|
|
|
|
|
|
|
|
// --- Process the uploaded PDF (e.g., extract form data, save to DB) --- |
|
|
|
|
|
//PDDocument document = PDDocument.load(targetLocation.toFile()); |
|
|
|
|
|
PDDocument document = Loader.loadPDF(targetLocation.toFile()); // Changed! |
|
|
|
|
|
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); |
|
|
|
|
|
|
|
|
|
|
|
if (acroForm != null) { |
|
|
|
|
|
// Example: Iterate through fields and print values |
|
|
|
|
|
for (PDField field : acroForm.getFields()) { |
|
|
|
|
|
System.out.println("Field: " + field.getPartialName() + ", Value: " + field.getValueAsString()); |
|
|
|
|
|
// You can save this data to a database, or perform other operations |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
document.close(); |
|
|
|
|
|
// ------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok("File uploaded and processed successfully: " + fileName); |
|
|
|
|
|
} catch (IOException ex) { |
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not upload or process file: " + ex.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Optional: Endpoint to upload only the form data (FDF/XFDF) |
|
|
|
|
|
@PostMapping("/upload/data") |
|
|
|
|
|
public ResponseEntity<String> uploadFormData(@RequestBody Map<String, String> formData) { |
|
|
|
|
|
// This approach requires the frontend to extract form data and send it as JSON |
|
|
|
|
|
// On the backend, you'd then use this data to populate the original PDF template |
|
|
|
|
|
// and save a new PDF or just the data in your database. |
|
|
|
|
|
// This is often more efficient than re-uploading the entire PDF. |
|
|
|
|
|
|
|
|
|
|
|
// Example: Logic to process form data |
|
|
|
|
|
System.out.println("Received form data: " + formData); |
|
|
|
|
|
// You would then load your template PDF, fill it with this data, and save. |
|
|
|
|
|
return ResponseEntity.ok("Form data received and processed."); |
|
|
|
|
|
} |
|
|
|
|
|
} |