2023-04-16 22:03:30 +01:00
|
|
|
package stirling.software.SPDF.utils;
|
2023-04-22 12:51:01 +01:00
|
|
|
|
2023-04-16 22:03:30 +01:00
|
|
|
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.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2025-01-06 12:41:30 +00:00
|
|
|
import java.util.Objects;
|
2023-04-16 22:03:30 +01:00
|
|
|
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.HttpStatus;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
2023-04-22 12:51:01 +01:00
|
|
|
|
2024-02-07 21:40:33 -05:00
|
|
|
import io.github.pixee.security.Filenames;
|
2024-06-02 12:02:01 +01:00
|
|
|
|
2024-12-17 10:26:18 +01:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2023-07-29 13:53:30 +01:00
|
|
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
|
|
|
|
2024-12-17 10:26:18 +01:00
|
|
|
@Slf4j
|
2023-04-16 22:03:30 +01:00
|
|
|
public class PDFToFile {
|
2024-03-29 17:02:33 -04:00
|
|
|
|
|
|
|
public ResponseEntity<byte[]> processPdfToHtml(MultipartFile inputFile)
|
|
|
|
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 = Filenames.toSimpleFileName(inputFile.getOriginalFilename());
|
2024-05-26 15:31:34 +01:00
|
|
|
String pdfBaseName = originalPdfFileName;
|
|
|
|
if (originalPdfFileName.contains(".")) {
|
|
|
|
pdfBaseName = originalPdfFileName.substring(0, originalPdfFileName.lastIndexOf('.'));
|
|
|
|
}
|
2024-03-29 17:02:33 -04:00
|
|
|
|
|
|
|
Path tempInputFile = null;
|
|
|
|
Path tempOutputDir = null;
|
|
|
|
byte[] fileBytes;
|
|
|
|
String fileName = "temp.file";
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Save the uploaded file to a temporary location
|
|
|
|
tempInputFile = Files.createTempFile("input_", ".pdf");
|
2024-05-27 17:53:18 +01:00
|
|
|
inputFile.transferTo(tempInputFile);
|
2024-03-29 17:02:33 -04:00
|
|
|
|
|
|
|
// Prepare the output directory
|
|
|
|
tempOutputDir = Files.createTempDirectory("output_");
|
|
|
|
|
|
|
|
// Run the pdftohtml command with complex output
|
|
|
|
List<String> command =
|
|
|
|
new ArrayList<>(
|
|
|
|
Arrays.asList(
|
|
|
|
"pdftohtml", "-c", tempInputFile.toString(), pdfBaseName));
|
|
|
|
|
|
|
|
ProcessExecutorResult returnCode =
|
|
|
|
ProcessExecutor.getInstance(ProcessExecutor.Processes.PDFTOHTML)
|
|
|
|
.runCommandWithOutputHandling(command, tempOutputDir.toFile());
|
|
|
|
|
|
|
|
// Get output files
|
2025-01-06 12:41:30 +00:00
|
|
|
File[] outputFiles = Objects.requireNonNull(tempOutputDir.toFile().listFiles());
|
2024-03-29 17:02:33 -04:00
|
|
|
|
|
|
|
// Return output files in a ZIP archive
|
|
|
|
fileName = pdfBaseName + "ToHtml.zip";
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
2024-06-02 11:59:43 +01:00
|
|
|
try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
|
|
|
|
for (File outputFile : outputFiles) {
|
|
|
|
ZipEntry entry = new ZipEntry(outputFile.getName());
|
|
|
|
zipOutputStream.putNextEntry(entry);
|
|
|
|
try (FileInputStream fis = new FileInputStream(outputFile)) {
|
|
|
|
IOUtils.copy(fis, zipOutputStream);
|
|
|
|
} catch (IOException e) {
|
2024-12-17 10:26:18 +01:00
|
|
|
log.error("Exception writing zip entry", e);
|
2024-06-02 11:59:43 +01:00
|
|
|
}
|
|
|
|
zipOutputStream.closeEntry();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2024-12-17 10:26:18 +01:00
|
|
|
log.error("Exception writing zip", e);
|
2024-03-29 17:02:33 -04:00
|
|
|
}
|
|
|
|
fileBytes = byteArrayOutputStream.toByteArray();
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
// Clean up the temporary files
|
2024-05-27 17:53:18 +01:00
|
|
|
if (tempInputFile != null) Files.deleteIfExists(tempInputFile);
|
2024-03-29 17:02:33 -04:00
|
|
|
if (tempOutputDir != null) FileUtils.deleteDirectory(tempOutputDir.toFile());
|
|
|
|
}
|
|
|
|
|
|
|
|
return WebResponseUtils.bytesToWebResponse(
|
|
|
|
fileBytes, fileName, MediaType.APPLICATION_OCTET_STREAM);
|
|
|
|
}
|
|
|
|
|
2023-04-22 12:51:01 +01:00
|
|
|
public ResponseEntity<byte[]> processPdfToOfficeFormat(
|
|
|
|
MultipartFile inputFile, String outputFormat, String libreOfficeFilter)
|
|
|
|
throws IOException, InterruptedException {
|
2023-04-16 22:03:30 +01:00
|
|
|
|
|
|
|
if (!"application/pdf".equals(inputFile.getContentType())) {
|
|
|
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the original PDF file name without the extension
|
2024-02-01 23:48:27 +00:00
|
|
|
String originalPdfFileName = Filenames.toSimpleFileName(inputFile.getOriginalFilename());
|
2023-04-16 22:03:30 +01:00
|
|
|
|
2024-05-26 15:31:34 +01:00
|
|
|
if (originalPdfFileName == null || "".equals(originalPdfFileName.trim())) {
|
|
|
|
originalPdfFileName = "output.pdf";
|
|
|
|
}
|
|
|
|
// Assume file is pdf if no extension
|
|
|
|
String pdfBaseName = originalPdfFileName;
|
|
|
|
if (originalPdfFileName.contains(".")) {
|
|
|
|
pdfBaseName = originalPdfFileName.substring(0, originalPdfFileName.lastIndexOf('.'));
|
|
|
|
}
|
2023-04-16 22:03:30 +01:00
|
|
|
// Validate output format
|
2023-04-22 12:51:01 +01:00
|
|
|
List<String> allowedFormats =
|
2024-03-29 17:02:33 -04:00
|
|
|
Arrays.asList("doc", "docx", "odt", "ppt", "pptx", "odp", "rtf", "xml", "txt:Text");
|
2023-04-16 22:03:30 +01:00
|
|
|
if (!allowedFormats.contains(outputFormat)) {
|
|
|
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
|
|
}
|
2023-04-22 12:51:01 +01:00
|
|
|
|
2023-04-16 22:03:30 +01:00
|
|
|
Path tempInputFile = null;
|
|
|
|
Path tempOutputDir = null;
|
|
|
|
byte[] fileBytes;
|
2023-05-01 21:57:48 +01:00
|
|
|
String fileName = "temp.file";
|
2023-04-16 22:03:30 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Save the uploaded file to a temporary location
|
|
|
|
tempInputFile = Files.createTempFile("input_", ".pdf");
|
2024-05-27 17:53:18 +01:00
|
|
|
inputFile.transferTo(tempInputFile);
|
2023-04-16 22:03:30 +01:00
|
|
|
|
|
|
|
// Prepare the output directory
|
|
|
|
tempOutputDir = Files.createTempDirectory("output_");
|
|
|
|
|
|
|
|
// Run the LibreOffice command
|
2023-04-22 12:51:01 +01:00
|
|
|
List<String> command =
|
|
|
|
new ArrayList<>(
|
|
|
|
Arrays.asList(
|
|
|
|
"soffice",
|
2024-06-14 01:13:38 +08:00
|
|
|
"--headless",
|
|
|
|
"--nologo",
|
2023-04-22 12:51:01 +01:00
|
|
|
"--infilter=" + libreOfficeFilter,
|
|
|
|
"--convert-to",
|
|
|
|
outputFormat,
|
|
|
|
"--outdir",
|
|
|
|
tempOutputDir.toString(),
|
|
|
|
tempInputFile.toString()));
|
2023-07-29 13:53:30 +01:00
|
|
|
ProcessExecutorResult returnCode =
|
|
|
|
ProcessExecutor.getInstance(ProcessExecutor.Processes.LIBRE_OFFICE)
|
|
|
|
.runCommandWithOutputHandling(command);
|
2023-04-16 22:03:30 +01:00
|
|
|
|
|
|
|
// Get output files
|
|
|
|
List<File> outputFiles = Arrays.asList(tempOutputDir.toFile().listFiles());
|
|
|
|
|
|
|
|
if (outputFiles.size() == 1) {
|
|
|
|
// Return single output file
|
|
|
|
File outputFile = outputFiles.get(0);
|
2024-02-02 00:29:18 +00:00
|
|
|
if ("txt:Text".equals(outputFormat)) {
|
2023-04-22 12:51:01 +01:00
|
|
|
outputFormat = "txt";
|
2023-04-16 22:03:30 +01:00
|
|
|
}
|
2023-05-01 21:57:48 +01:00
|
|
|
fileName = pdfBaseName + "." + outputFormat;
|
2023-04-16 22:03:30 +01:00
|
|
|
fileBytes = FileUtils.readFileToByteArray(outputFile);
|
|
|
|
} else {
|
|
|
|
// Return output files in a ZIP archive
|
2024-06-02 12:02:01 +01:00
|
|
|
fileName = pdfBaseName + "To" + outputFormat + ".zip";
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
|
|
|
|
for (File outputFile : outputFiles) {
|
|
|
|
ZipEntry entry = new ZipEntry(outputFile.getName());
|
|
|
|
zipOutputStream.putNextEntry(entry);
|
|
|
|
try (FileInputStream fis = new FileInputStream(outputFile)) {
|
|
|
|
IOUtils.copy(fis, zipOutputStream);
|
|
|
|
} catch (IOException e) {
|
2024-12-17 10:26:18 +01:00
|
|
|
log.error("Exception writing zip entry", e);
|
2024-06-02 12:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
zipOutputStream.closeEntry();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2024-12-17 10:26:18 +01:00
|
|
|
log.error("Exception writing zip", e);
|
2024-06-02 12:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fileBytes = byteArrayOutputStream.toByteArray();
|
2023-04-16 22:03:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
// Clean up the temporary files
|
2024-05-27 17:53:18 +01:00
|
|
|
Files.deleteIfExists(tempInputFile);
|
2023-04-16 22:03:30 +01:00
|
|
|
if (tempOutputDir != null) FileUtils.deleteDirectory(tempOutputDir.toFile());
|
|
|
|
}
|
2023-05-31 20:15:48 +01:00
|
|
|
return WebResponseUtils.bytesToWebResponse(
|
|
|
|
fileBytes, fileName, MediaType.APPLICATION_OCTET_STREAM);
|
2023-04-16 22:03:30 +01:00
|
|
|
}
|
|
|
|
}
|