Quarkus PDF Generation Service

Job ID: 39595175

Budget: $15 – $25 USD

Make quarkus service to fully generate provided pdfs (all templates and their data must be created by you as output result along with service code) by taking a template and data provided in JSON format.:


--------------------------------------------------------------------------
// Endpoint to generate PDF from a template and data provided in JSON format
// Example request to the endpoint:
POST http://127.0.0.1:8080/generatepdf
Content-Type: application/json

{
"template": "<html><body><h1>Hello ${name}</h1><p>Your score: ${score}</p></body></html>",
"data": {
"name": "John Doe",
"score": 95
}
}
--------------------------------------------------------------------------
Here is code template that can be used for a REST endpoint to generate a PDF from a template and data provided in JSON format. This example uses the Qute templating engine and iText for PDF generation.:
--------------------------------------------------------------------------
@POST
@Path("/generatepdf")
@Produces("application/pdf")
@Consumes(MediaType.APPLICATION_JSON)
public Response generatePdf(jakarta.json.JsonObject jsonRequest) {
try {
String template = jsonRequest.getString("template");
String outputFileName = jsonRequest.getString("output_file_name");
jakarta.json.JsonObject jsonData = jsonRequest.getJsonObject("data");

// Convert JsonObject to Map<String, Object>
Map<String, Object> data = new HashMap<>();
for (String key : jsonData.keySet()) {
data.put(key, jsonData.get(key));
}

byte[] pdfData = pdfService.generatePdf(template, data);

if (pdfData == null || pdfData.length == 0) {
return Response.serverError()
.entity("Failed to generate PDF.")
.build();
}

// Return response with correct headers for file download
return Response.ok(pdfData)
.type("application/pdf")
.header("Content-Disposition", "attachment; filename=\"" + outputFileName + "\"")
.build();

} catch (Exception e) {
return Response.serverError()
.entity("Error generating PDF: " + e.getMessage())
.build();
}
}


--------------------------------------------------------------------------
and the service class can be like this (you can use any pdf or html library you want) :
--------------------------------------------------------------------------

import io.quarkus.qute.Engine;
import io.quarkus.qute.TemplateInstance;
import com.itextpdf.html2pdf.HtmlConverter;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;

import java.io.ByteArrayOutputStream;
import java.util.Map;

@ApplicationScoped
public class PdfService {

@Inject
Engine engine; // Inject Qute Engine

private static final Logger LOG = Logger.getLogger(PdfService.class);

public byte[] generatePdf(String templateContent, Map<String, Object> data) {
try {
// 1. Parse the template dynamically
TemplateInstance renderedTemplate = engine.parse(templateContent).data(data);

// 2. Render the final HTML
String finalHtml = renderedTemplate.render();

// 3. Convert HTML to PDF using iText
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
HtmlConverter.convertToPdf(finalHtml, pdfOutputStream);

return pdfOutputStream.toByteArray();

} catch (Exception e) {
LOG.error("Error generating PDF", e);
return null;
}
}
}


Also we will need three templates to be created, where we will mark unique fields, the fields are overlapping, but overall there is around 45 unique fields, with each of templates being two pages size.
Related categories: HTML JSON REST API Quarkus