What is Spring Boot?
Spring Boot is a framework for efficiently building production level Java apps. It builds on top of the Spring framework by offering out of the box configuration and setup. It allows developers to build and deploy enterprise microservices and APIs with minimal tweaking required.
How to process PDF files?
JPedal is our Java PDF SDK which enables developers to convert, manipulate, print, and view PDF files within Java applications. It provides APIs for rendering PDFs to images, extracting text and metadata, manipulating pages, and much more! JPedal supports both server side and desktop use cases, making it suitable for integrating PDF functionality into web applications, or document processing pipelines.
Spring Boot PDF processing
Combining JPedal with Spring Boot allows developers to build web services which require processing PDFs. For example, a Spring Boot REST API could use JPedal to convert uploaded PDFs into images.
This provides the following benefits:
- Scalability: Spring Boot handles concurrency and deployment, while JPedal manages the PDF processing
- Simplicity: Minimal configuration for both the web layer (Spring Boot) and document layer (JPedal)
- Extensibility: Easy to integrate with databases or other cloud services
Together, JPedal and Spring Boot form a robust stack for creating PDF driven Java applications with web capabilities.
How to set up a Spring Boot project with JPedal
Now we are going to make a simple microservice which converts PDFs into PNGs.
First, visit Sprint Initializr and create a new project. Add ‘Spring Web’ as a dependency.
Second, you will need to download a JPedal jar, and add it to your Java project.
Next, create a service class which will perform the PDF to PNG conversion:
@Service
public class PdfService {
public File renderPdfToImage(final File pdfFile, final int page) throws Exception {
final File outputDir = new File("output");
if (!outputDir.exists()) {
outputDir.mkdirs();
}
final ConvertPagesToImages convert = new ConvertPagesToImages(pdfFile.getAbsolutePath());
if (!convert.openPDFFile()) {
convert.closePDFfile();
}
final BufferedImage bi = convert.getPageAsImage(page);
final File out = Paths.get(outputDir.getAbsolutePath(),
pdfFile.getName() + "-" + page + ".png").toFile();
final PngEncoderOptions options = new PngEncoderOptions();
JDeli.write(bi, options, out);
convert.closePDFfile();
return out;
}
}
Then create a new controller class which will handle the REST requests:
@RestController
@RequestMapping("/pdf")
public class PdfController {
private final PdfService pdfService;
public PdfController(final PdfService pdfService) {
this.pdfService = pdfService;
}
@PostMapping(value = "/convert", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity convertPdf(@RequestParam("file") final MultipartFile file,
@RequestParam("page") final int page) throws Exception {
final File temp = File.createTempFile("upload", ".pdf");
file.transferTo(temp);
final File output = pdfService.renderPdfToImage(temp, page);
final InputStreamResource resource = new InputStreamResource(new FileInputStream(output));
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + output.getName() + "\"")
.contentLength(output.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}
}
Finally, update the application.properties file to include the following settings:
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=20MB
spring.servlet.multipart.max-request-size=20MB
Now you can start the microservice using:
./mvnw spring-boot:run
Once the build completes, you can send a PDF to the microservice and have it converted:
curl -X POST http://localhost:8080/pdf/convert -F file=@/Users/Shared/inputFile.pdf -F page=1 -o output.png
Source code
All of the code in this article can be found on our GitHub: jpedal-springboot-example.
Download JPedal now
You may download a trial copy of JPedal to get familiar with how it works.
Learn more
Find out what else you can do with JPedal.
We can help you better understand the PDF format as developers who have been working with the format for more than 2 decades!