PPT to PDF Converter
Convert your PowerPoint presentations to PDF format
๐
Drag & drop your PPT/PPTX file here
๐
presentation.pptx
0%
Uploading...
Backend Implementation Code
Controller
POM.xml
Application
package com.example.ppt2pdf.controller; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.beans.factory.annotation.Value; import java.io.*; import java.nio.file.*; import java.util.UUID; @RestController @RequestMapping("/api") @CrossOrigin(origins = "*") public class ConvertController { @Value("${app.temp-dir:/tmp/ppt2pdf}") private String tempDir; @PostMapping("/convert") public ResponseEntityconvertToPdf(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return ResponseEntity.badRequest().body("No file uploaded".getBytes()); } String originalFilename = file.getOriginalFilename(); if (originalFilename == null || (!originalFilename.toLowerCase().endsWith(".ppt") && !originalFilename.toLowerCase().endsWith(".pptx"))) { return ResponseEntity.badRequest().body("Invalid file type. Please upload a PPT or PPTX file.".getBytes()); } String sessionId = UUID.randomUUID().toString(); Path workingDir = Paths.get(tempDir, sessionId); try { Files.createDirectories(workingDir); // Save uploaded file Path inputFile = workingDir.resolve(originalFilename); Files.copy(file.getInputStream(), inputFile, StandardCopyOption.REPLACE_EXISTING); // Convert using LibreOffice ProcessBuilder processBuilder = new ProcessBuilder( "soffice", "--headless", "--convert-to", "pdf", "--outdir", workingDir.toString(), inputFile.toString() ); Process process = processBuilder.start(); int exitCode = process.waitFor(); if (exitCode != 0) { throw new RuntimeException("LibreOffice conversion failed with exit code: " + exitCode); } // Find the converted PDF file String baseName = originalFilename.replaceFirst("[.][^.]+$", ""); Path pdfFile = workingDir.resolve(baseName + ".pdf"); if (!Files.exists(pdfFile)) { // Try to find any PDF file in the directory try (DirectoryStream stream = Files.newDirectoryStream(workingDir, "*.pdf")) { for (Path entry : stream) { pdfFile = entry; break; } } if (!Files.exists(pdfFile)) { throw new RuntimeException("Converted PDF file not found"); } } // Read the PDF file byte[] pdfBytes = Files.readAllBytes(pdfFile); // Set response headers HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_PDF); headers.setContentDispositionFormData("attachment", baseName + ".pdf"); headers.setContentLength(pdfBytes.length); return ResponseEntity.ok() .headers(headers) .body(pdfBytes); } catch (Exception e) { return ResponseEntity.internalServerError() .body(("Conversion failed: " + e.getMessage()).getBytes()); } finally { // Clean up temporary files try { deleteDirectory(workingDir.toFile()); } catch (IOException e) { System.err.println("Failed to clean up temporary directory: " + e.getMessage()); } } } private void deleteDirectory(File directory) throws IOException { if (directory.exists()) { File[] files = directory.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { deleteDirectory(file); } else { Files.deleteIfExists(file.toPath()); } } } Files.deleteIfExists(directory.toPath()); } } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>ppt2pdf</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.4</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.example.ppt2pdf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Ppt2PdfApplication { public static void main(String[] args) { SpringApplication.run(Ppt2PdfApplication.class, args); } }