Skip to content

Commit

Permalink
Load the font files found under resources/pdf-fonts
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Gonzalez <[email protected]>

Use them as substitution fonts
  • Loading branch information
vrajmohan committed Nov 28, 2023
1 parent eb1990c commit 16b6409
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/main/java/formflow/library/pdf/PDFFormFiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
Expand All @@ -29,7 +35,7 @@ public File fill(String pdfTemplatePath, Collection<PdfField> fields, boolean fl
File outputFile = File.createTempFile("Filled_", ".pdf");
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream(outputFile));
AcroFields acroFields = pdfStamper.getAcroFields();
configureSubstituteFonts(acroFields);
configureSubstitutionFonts(acroFields);
for (PdfField pdfField : fields) {
acroFields.setField(pdfField.name(), pdfField.value());
}
Expand Down Expand Up @@ -57,8 +63,9 @@ public File fill(String pdfTemplatePath, Collection<PdfField> fields) {
* Configure the fonts to be used if the default fonts do not support the characters in the values to be filled
* @param acroFields the `AcroFields` object (the form) in the PDF template
*/
private static void configureSubstituteFonts(AcroFields acroFields) {
Stream.of("NotoSans-Regular.ttf", "NotoSansSC-Regular.ttf").forEach( fontResource -> {
private static void configureSubstitutionFonts(AcroFields acroFields) {
Set<String> files = getFontFiles();
files.forEach(fontResource -> {
BaseFont font = null;
try {
font = BaseFont.createFont(fontResource, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Expand All @@ -68,4 +75,23 @@ private static void configureSubstituteFonts(AcroFields acroFields) {
acroFields.addSubstitutionFont(font);
});
}

/**
* Get the list of font files to be used as substitution fonts from resources/pdf-fonts
* @return Set of filenames with paths
*/
@NotNull
private static Set<String> getFontFiles() {
URI directoryURI = null;
try {
directoryURI = new ClassPathResource("pdf-fonts").getURI();
} catch (IOException e) {
log.warn("Failed to find pdf-fonts directory: %s", e);
}

return Stream.of(Objects.requireNonNull(new File(directoryURI).listFiles()))
.filter(file -> !file.isDirectory())
.map(File::getPath)
.collect(Collectors.toSet());
}
}
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions src/test/java/formflow/library/pdf/PDFFormFillerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.io.File;
import java.io.IOException;
Expand All @@ -14,6 +16,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ActiveProfiles("test")
@SpringBootTest(properties = {"form-flow.path=flows-config/test-flow.yaml"})
class PDFFormFillerTest {

private final PDFFormFiller pdfFormFiller = new PDFFormFiller();
Expand Down

0 comments on commit 16b6409

Please sign in to comment.