Refactor EML to PDF conversion to use CustomPDFDocumentFactory

This commit is contained in:
Balázs Szücs 2025-06-07 10:21:37 +02:00
parent c5da668f9b
commit c3af920db5
2 changed files with 12 additions and 6 deletions

View File

@ -20,7 +20,6 @@ import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
@ -88,7 +87,8 @@ public class EmlToPdf {
EmlToPdfRequest request,
byte[] emlBytes,
String fileName,
boolean disableSanitize)
boolean disableSanitize,
stirling.software.common.service.CustomPDFDocumentFactory pdfDocumentFactory)
throws IOException, InterruptedException {
EmlToPdf.fileName = fileName;
@ -145,7 +145,7 @@ public class EmlToPdf {
&& !emailContent.getAttachments().isEmpty()) {
try {
pdfBytes = attachFilesToPdf(pdfBytes, emailContent.getAttachments());
pdfBytes = attachFilesToPdf(pdfBytes, emailContent.getAttachments(), pdfDocumentFactory);
} catch (IOException e) {
// Continue with PDF without attachments rather than failing completely
log.warn("Failed to attach files to PDF: {}", e.getMessage());
@ -997,9 +997,9 @@ public class EmlToPdf {
return html.toString();
}
private static byte[] attachFilesToPdf(byte[] pdfBytes, List<EmailAttachment> attachments)
private static byte[] attachFilesToPdf(byte[] pdfBytes, List<EmailAttachment> attachments, stirling.software.common.service.CustomPDFDocumentFactory pdfDocumentFactory)
throws IOException {
try (PDDocument document = Loader.loadPDF(pdfBytes);
try (PDDocument document = pdfDocumentFactory.load(pdfBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
if (attachments == null || attachments.isEmpty()) {

View File

@ -17,8 +17,10 @@ import org.springframework.web.multipart.MultipartFile;
import io.github.pixee.security.Filenames;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import stirling.software.common.model.api.converters.EmlToPdfRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.EmlToPdf;
import stirling.software.common.util.WebResponseUtils;
@ -26,10 +28,13 @@ import stirling.software.common.util.WebResponseUtils;
@RequestMapping("/api/v1/convert")
@Tag(name = "Convert", description = "Convert APIs")
@Slf4j
@RequiredArgsConstructor
public class ConvertEmlToPDF {
@Value("${WEASYPRINT_PATH:weasyprint}")
private String weasyprintPath;
private final CustomPDFDocumentFactory pdfDocumentFactory;
@PostMapping(consumes = "multipart/form-data", value = "/eml/pdf")
@Operation(
@ -95,7 +100,8 @@ public class ConvertEmlToPDF {
request,
fileBytes,
originalFilename,
false);
false,
pdfDocumentFactory);
if (pdfBytes == null || pdfBytes.length == 0) {
log.error("PDF conversion failed - empty output for {}", originalFilename);