-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert PDF to Docx, powerpoint and others (#90)
- Loading branch information
Showing
23 changed files
with
626 additions
and
40 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
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
97 changes: 97 additions & 0 deletions
97
src/main/java/stirling/software/SPDF/controller/converters/ConvertPDFToOffice.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,97 @@ | ||
package stirling.software.SPDF.controller.converters; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
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; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import stirling.software.SPDF.utils.PDFToFile; | ||
|
||
@Controller | ||
public class ConvertPDFToOffice { | ||
|
||
|
||
|
||
@GetMapping("/pdf-to-word") | ||
public ModelAndView pdfToWord() { | ||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-word"); | ||
modelAndView.addObject("currentPage", "pdf-to-word"); | ||
return modelAndView; | ||
} | ||
|
||
@GetMapping("/pdf-to-presentation") | ||
public ModelAndView pdfToPresentation() { | ||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-presentation"); | ||
modelAndView.addObject("currentPage", "pdf-to-presentation"); | ||
return modelAndView; | ||
} | ||
|
||
@GetMapping("/pdf-to-text") | ||
public ModelAndView pdfToText() { | ||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-text"); | ||
modelAndView.addObject("currentPage", "pdf-to-text"); | ||
return modelAndView; | ||
} | ||
|
||
@GetMapping("/pdf-to-html") | ||
public ModelAndView pdfToHTML() { | ||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-html"); | ||
modelAndView.addObject("currentPage", "pdf-to-html"); | ||
return modelAndView; | ||
} | ||
|
||
@GetMapping("/pdf-to-xml") | ||
public ModelAndView pdfToXML() { | ||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-xml"); | ||
modelAndView.addObject("currentPage", "pdf-to-xml"); | ||
return modelAndView; | ||
} | ||
|
||
|
||
@PostMapping("/pdf-to-word") | ||
public ResponseEntity<byte[]> processPdfToWord(@RequestParam("fileInput") MultipartFile inputFile, | ||
@RequestParam("outputFormat") String outputFormat) throws IOException, InterruptedException { | ||
PDFToFile pdfToFile = new PDFToFile(); | ||
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import"); | ||
} | ||
|
||
@PostMapping("/pdf-to-presentation") | ||
public ResponseEntity<byte[]> processPdfToPresentation(@RequestParam("fileInput") MultipartFile inputFile, | ||
@RequestParam("outputFormat") String outputFormat) throws IOException, InterruptedException { | ||
PDFToFile pdfToFile = new PDFToFile(); | ||
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "impress_pdf_import"); | ||
} | ||
|
||
@PostMapping("/pdf-to-text") | ||
public ResponseEntity<byte[]> processPdfToRTForTXT(@RequestParam("fileInput") MultipartFile inputFile, | ||
@RequestParam("outputFormat") String outputFormat) throws IOException, InterruptedException { | ||
PDFToFile pdfToFile = new PDFToFile(); | ||
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import"); | ||
} | ||
|
||
|
||
@PostMapping("/pdf-to-html") | ||
public ResponseEntity<byte[]> processPdfToHTML(@RequestParam("fileInput") MultipartFile inputFile) throws IOException, InterruptedException { | ||
PDFToFile pdfToFile = new PDFToFile(); | ||
return pdfToFile.processPdfToOfficeFormat(inputFile, "html", "writer_pdf_import"); | ||
} | ||
|
||
@PostMapping("/pdf-to-xml") | ||
public ResponseEntity<byte[]> processPdfToXML(@RequestParam("fileInput") MultipartFile inputFile) throws IOException, InterruptedException { | ||
PDFToFile pdfToFile = new PDFToFile(); | ||
return pdfToFile.processPdfToOfficeFormat(inputFile, "xml", "writer_pdf_import"); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |
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
101 changes: 101 additions & 0 deletions
101
src/main/java/stirling/software/SPDF/utils/PDFToFile.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,101 @@ | ||
package stirling.software.SPDF.utils; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.IOUtils; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.multipart.MultipartFile; | ||
public class PDFToFile { | ||
public ResponseEntity<byte[]> processPdfToOfficeFormat(MultipartFile inputFile, String outputFormat, String libreOfficeFilter) | ||
throws IOException, InterruptedException { | ||
|
||
if (!"application/pdf".equals(inputFile.getContentType())) { | ||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
// Get the original PDF file name without the extension | ||
String originalPdfFileName = inputFile.getOriginalFilename(); | ||
String pdfBaseName = originalPdfFileName.substring(0, originalPdfFileName.lastIndexOf('.')); | ||
|
||
// Validate output format | ||
List<String> allowedFormats = Arrays.asList("doc", "docx", "odt", "ppt", "pptx", "odp", "rtf", "html","xml","txt:Text"); | ||
if (!allowedFormats.contains(outputFormat)) { | ||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
Path tempInputFile = null; | ||
Path tempOutputDir = null; | ||
byte[] fileBytes; | ||
// Prepare response | ||
HttpHeaders headers = new HttpHeaders(); | ||
|
||
try { | ||
// Save the uploaded file to a temporary location | ||
tempInputFile = Files.createTempFile("input_", ".pdf"); | ||
Files.copy(inputFile.getInputStream(), tempInputFile, StandardCopyOption.REPLACE_EXISTING); | ||
|
||
// Prepare the output directory | ||
tempOutputDir = Files.createTempDirectory("output_"); | ||
|
||
// Run the LibreOffice command | ||
List<String> command = new ArrayList<>(Arrays.asList( | ||
"soffice", "--infilter=" + libreOfficeFilter, "--convert-to", outputFormat, "--outdir", tempOutputDir.toString(), tempInputFile.toString() | ||
)); | ||
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.LIBRE_OFFICE).runCommandWithOutputHandling(command); | ||
|
||
// Get output files | ||
List<File> outputFiles = Arrays.asList(tempOutputDir.toFile().listFiles()); | ||
|
||
if (outputFiles.size() == 1) { | ||
// Return single output file | ||
File outputFile = outputFiles.get(0); | ||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); | ||
if(outputFormat.equals("txt:Text")) { | ||
outputFormat="txt"; | ||
} | ||
headers.setContentDispositionFormData("attachment", pdfBaseName + "." + outputFormat); | ||
fileBytes = FileUtils.readFileToByteArray(outputFile); | ||
} else { | ||
// Return output files in a ZIP archive | ||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); | ||
headers.setContentDispositionFormData("attachment", pdfBaseName + "To" + outputFormat + ".zip"); | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream); | ||
|
||
for (File outputFile : outputFiles) { | ||
ZipEntry entry = new ZipEntry(outputFile.getName()); | ||
zipOutputStream.putNextEntry(entry); | ||
FileInputStream fis = new FileInputStream(outputFile); | ||
IOUtils.copy(fis, zipOutputStream); | ||
fis.close(); | ||
zipOutputStream.closeEntry(); | ||
} | ||
|
||
zipOutputStream.close(); | ||
fileBytes = byteArrayOutputStream.toByteArray(); | ||
} | ||
|
||
} finally { | ||
// Clean up the temporary files | ||
if (tempInputFile != null) | ||
Files.delete(tempInputFile); | ||
if (tempOutputDir != null) | ||
FileUtils.deleteDirectory(tempOutputDir.toFile()); | ||
} | ||
return new ResponseEntity<>(fileBytes, headers, HttpStatus.OK); | ||
} | ||
} |
Oops, something went wrong.