Project: pdf-generator This single-file view contains all the files you need to create a Spring Boot web app that serves an HTML page and a Java endpoint which generates a downloadable PDF using Apache PDFBox. --- pom.xml 4.0.0 com.example pdf-generator 0.0.1-SNAPSHOT jar pdf-generator Spring Boot PDF generator using PDFBox org.springframework.boot spring-boot-starter-parent 3.1.4 17 org.springframework.boot spring-boot-starter-web org.apache.pdfbox pdfbox 2.0.30 jakarta.servlet jakarta.servlet-api provided org.springframework.boot spring-boot-maven-plugin --- src/main/java/com/example/pdfgenerator/PdfGeneratorApplication.java package com.example.pdfgenerator; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class PdfGeneratorApplication { public static void main(String[] args) { SpringApplication.run(PdfGeneratorApplication.class, args); } } --- src/main/java/com/example/pdfgenerator/controller/PdfController.java package com.example.pdfgenerator.controller; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.ByteArrayOutputStream; import java.io.IOException; @RestController public class PdfController { @PostMapping(path = "/generate-pdf", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public ResponseEntity generatePdf(@RequestParam String title, @RequestParam String content) throws IOException { // Create PDF in memory using PDFBox try (PDDocument doc = new PDDocument(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { PDPage page = new PDPage(PDRectangle.A4); doc.addPage(page); try (PDPageContentStream cs = new PDPageContentStream(doc, page)) { float margin = 50; float yStart = PDRectangle.A4.getHeight() - margin; float leading = 14.5f; cs.beginText(); cs.setFont(PDType1Font.HELVETICA_BOLD, 18); cs.newLineAtOffset(margin, yStart); cs.showText(title); cs.endText(); // Body text cs.beginText(); cs.setFont(PDType1Font.HELVETICA, 12); cs.newLineAtOffset(margin, yStart - 30); String[] lines = wrapText(content, 80); // simple wrap based on chars for (String line : lines) { cs.showText(line); cs.newLineAtOffset(0, -leading); } cs.endText(); } doc.save(baos); byte[] pdfBytes = baos.toByteArray(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_PDF); headers.setContentDispositionFormData("attachment", "generated.pdf"); headers.setContentLength(pdfBytes.length); return ResponseEntity.ok() .headers(headers) .body(pdfBytes); } } // Very simple wrapper — for production you would use better text layout logic private String[] wrapText(String text, int maxCharsPerLine) { return text.replaceAll("\r", "") .split("(?<=\G.{" + maxCharsPerLine + "})"); } } --- src/main/resources/static/index.html Generate PDF

Downloadable PDF Generator

--- How to build & run 1. Save the project with the given file structure. 2. Ensure you have Java 17+ and Maven installed. 3. From project root run: mvn clean package java -jar target/pdf-generator-0.0.1-SNAPSHOT.jar 4. Open your browser at: http://localhost:8080/ 5. Fill the form and click "Generate & Download PDF" — the browser will download generated.pdf --- Notes & Improvements The current text wrapping is basic; for multi-page layout or advanced typography use PDFBox's PDPageContentStream text wrapping utilities or a higher-level library. If you prefer a quick non-Spring Java app, I can provide a Jetty-embedded example or a simple CLI program that writes a PDF file to disk. To include images or fonts, add resources and load them via PDFBox.

Comments