feat(pdf): integrate PDFium4j for PDF processing and metadata extraction (#281)

* feat(pdf): integrate PDFium4j for PDF processing and metadata extraction

* feat(pdf): update PDFium4j to version 0.12.0 and optimize XMP metadata handling

* feat(pdf): update PDFium4j to version 0.13.0

* feat(pdf): enhance cross-compilation support and improve XML parsing in metadata extraction

* Delete dev.local-libs.docker-compose.yml

* feat(pdf): update PDFium4j to version 0.14.0
This commit is contained in:
brios
2026-04-22 17:27:01 -04:00
committed by Zach Yale
parent ad3a981ad1
commit 64083fe9b7
16 changed files with 547 additions and 851 deletions
@@ -26,6 +26,9 @@ import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import org.booklore.test.RequiresPdfium;
@RequiresPdfium
class PdfMetadataExtractorTest {
private PdfMetadataExtractor extractor;
@@ -1,171 +0,0 @@
package org.booklore.service.metadata.writer;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.type.TextType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class BookLoreSchemaTest {
private XMPMetadata xmpMetadata;
private BookLoreSchema schema;
@BeforeEach
void setUp() {
xmpMetadata = XMPMetadata.createXMPMetadata();
schema = new BookLoreSchema(xmpMetadata);
}
@Nested
class ScalarTextPropertiesTests {
@Test
void setSubtitle_addsPropertyToSchema() {
schema.setSubtitle("A Novel");
TextType property = (TextType) schema.getProperty(BookLoreSchema.SUBTITLE);
assertNotNull(property);
assertEquals("A Novel", property.getStringValue());
}
@Test
void setSubtitle_whenBlank_doesNotAddProperty() {
schema.setSubtitle(" ");
assertNull(schema.getProperty(BookLoreSchema.SUBTITLE));
}
@Test
void setIsbn10_addsPropertyToSchema() {
schema.setIsbn10("0123456789");
TextType property = (TextType) schema.getProperty(BookLoreSchema.ISBN_10);
assertNotNull(property);
assertEquals("0123456789", property.getStringValue());
}
@Test
void setLubimyczytacId_addsPropertyToSchema() {
schema.setLubimyczytacId("12345");
TextType property = (TextType) schema.getProperty(BookLoreSchema.LUBIMYCZYTAC_ID);
assertNotNull(property);
assertEquals("12345", property.getStringValue());
}
@Test
void setHardcoverBookId_addsPropertyToSchema() {
schema.setHardcoverBookId(98765);
TextType property = (TextType) schema.getProperty(BookLoreSchema.HARDCOVER_BOOK_ID);
assertNotNull(property);
assertEquals("98765", property.getStringValue());
}
}
@Nested
class RatingPropertiesTests {
@Test
void setRating_formatsWithLocaleUS() {
schema.setRating(8.5);
TextType property = (TextType) schema.getProperty(BookLoreSchema.RATING);
assertNotNull(property);
assertEquals("8.50", property.getStringValue());
assertFalse(property.getStringValue().contains(","));
}
@Test
void setAmazonRating_formatsCorrectly() {
schema.setAmazonRating(4.25);
TextType property = (TextType) schema.getProperty(BookLoreSchema.AMAZON_RATING);
assertNotNull(property);
assertEquals("4.25", property.getStringValue());
}
@Test
void setGoodreadsRating_formatsCorrectly() {
schema.setGoodreadsRating(3.99);
TextType property = (TextType) schema.getProperty(BookLoreSchema.GOODREADS_RATING);
assertNotNull(property);
assertEquals("3.99", property.getStringValue());
}
@Test
void setRating_whenNull_doesNotAddProperty() {
schema.setRating(null);
assertNull(schema.getProperty(BookLoreSchema.RATING));
}
}
@Nested
class TextCollectionPropertiesTests {
@Test
void setMoods_createsDelimitedText() {
schema.setMoods(Set.of("Dark", "Atmospheric", "Suspenseful"));
TextType property = (TextType) schema.getProperty(BookLoreSchema.MOODS);
assertNotNull(property);
String value = property.getStringValue();
assertTrue(value.contains("Dark"));
assertTrue(value.contains("Atmospheric"));
assertTrue(value.contains("Suspenseful"));
assertTrue(value.contains("; "));
}
@Test
void setMoods_whenEmpty_doesNotAddProperty() {
schema.setMoods(Set.of());
assertNull(schema.getProperty(BookLoreSchema.MOODS));
}
@Test
void setMoods_whenNull_doesNotAddProperty() {
schema.setMoods(null);
assertNull(schema.getProperty(BookLoreSchema.MOODS));
}
@Test
void setTags_createsDelimitedText() {
schema.setTags(Set.of("Fantasy", "Epic", "Magic"));
TextType property = (TextType) schema.getProperty(BookLoreSchema.TAGS);
assertNotNull(property);
String value = property.getStringValue();
assertTrue(value.contains("Fantasy"));
assertTrue(value.contains("Epic"));
assertTrue(value.contains("Magic"));
}
@Test
void setTags_filtersBlankValues() {
schema.setTags(Set.of("Fantasy", " ", "Epic"));
TextType property = (TextType) schema.getProperty(BookLoreSchema.TAGS);
assertNotNull(property);
assertFalse(property.getStringValue().contains(" "));
}
}
@Nested
class NamespaceTests {
@Test
void namespace_isCorrect() {
assertEquals("http://booklore.org/metadata/1.0/", BookLoreSchema.NAMESPACE);
}
@Test
void prefix_isCorrect() {
assertEquals("booklore", BookLoreSchema.PREFIX);
}
}
}
@@ -1,9 +1,7 @@
package org.booklore.service.metadata.writer;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.grimmory.pdfium4j.PdfDocument;
import org.grimmory.pdfium4j.model.PageSize;
import org.booklore.model.dto.BookMetadata;
import org.booklore.model.dto.settings.AppSettings;
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
@@ -21,7 +19,6 @@ import org.mockito.Mockito;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
@@ -33,6 +30,9 @@ import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
import org.booklore.test.RequiresPdfium;
@RequiresPdfium
class PdfMetadataWriterTest {
private PdfMetadataWriter writer;
@@ -375,9 +375,9 @@ class PdfMetadataWriterTest {
private File createEmptyPdf(String name) throws IOException {
File pdf = tempDir.resolve(name).toFile();
try (PDDocument doc = new PDDocument()) {
doc.addPage(new PDPage());
doc.save(pdf);
try (PdfDocument doc = PdfDocument.create()) {
doc.insertBlankPage(0, new PageSize(612, 792));
doc.save(pdf.toPath());
}
return pdf;
}
@@ -389,12 +389,9 @@ class PdfMetadataWriterTest {
}
private String readXmpContent(File pdf) throws IOException {
try (PDDocument doc = Loader.loadPDF(pdf)) {
PDMetadata metadata = doc.getDocumentCatalog().getMetadata();
if (metadata == null) {
return "";
}
return new String(metadata.toByteArray(), StandardCharsets.UTF_8);
try (PdfDocument doc = PdfDocument.open(pdf.toPath())) {
String xmp = doc.xmpMetadataString();
return xmp != null ? xmp : "";
}
}
}
@@ -0,0 +1,31 @@
package org.booklore.test;
import org.grimmory.pdfium4j.PdfiumLibrary;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
public class PdfiumAvailableCondition implements ExecutionCondition {
private static final ConditionEvaluationResult ENABLED =
ConditionEvaluationResult.enabled("PDFium native library is available");
private static final Boolean AVAILABLE = probe();
private static Boolean probe() {
try {
PdfiumLibrary.initialize();
return true;
} catch (Exception | UnsatisfiedLinkError e) {
return false;
}
}
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
if (Boolean.TRUE.equals(AVAILABLE)) {
return ENABLED;
}
return ConditionEvaluationResult.disabled("PDFium native library not available on this platform");
}
}
@@ -0,0 +1,16 @@
package org.booklore.test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.lang.annotation.*;
/**
* Marks a test class or method as requiring PDFium native binaries.
* Tests annotated with this will be skipped (not failed) if the
* PDFium native library is not available on the current platform.
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(PdfiumAvailableCondition.class)
public @interface RequiresPdfium {
}