Improve pdf paragraph constrains

The ParagraphPdfDocumentReader relies on a PDF object called 'outline' (e.g. TOC) to be present in the document.
If the pdf was not generated with TOC, the other options in Spring AI are PagePdfDocumentReader and TikaDocumentReader.

 Resolves #59
This commit is contained in:
Christian Tzolov
2024-01-13 12:16:06 +01:00
parent 408d75e0d4
commit 7b38cc3a88
3 changed files with 64 additions and 0 deletions

View File

@@ -110,6 +110,9 @@ public class ParagraphPdfDocumentReader implements DocumentReader {
this.resourceFileName = pdfResource.getFile();
}
catch (IllegalArgumentException iae) {
throw iae;
}
catch (Exception e) {
throw new RuntimeException(e);
}

View File

@@ -28,6 +28,7 @@ import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPa
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
@@ -75,6 +76,13 @@ public class ParagraphManager {
private final PDDocument document;
public ParagraphManager(PDDocument document) {
Assert.notNull(document, "PDDocument must not be null");
Assert.notNull(document.getDocumentCatalog().getDocumentOutline(),
"Document outline (e.g. TOC) is null. "
+ "Make sure the PDF document has a table of contents (TOC). If not, consider the "
+ "PagePdfDocumentReader or the TikaDocumentReader instead.");
try {
this.document = document;

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2024-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.reader.pdf;
import org.junit.jupiter.api.Test;
import org.springframework.ai.reader.ExtractedTextFormatter;
import org.springframework.ai.reader.pdf.config.PdfDocumentReaderConfig;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* @author Christian Tzolov
*/
public class ParagraphPdfDocumentReaderTests {
@Test
public void testPdfWithoutToc() {
assertThatThrownBy(() -> {
new ParagraphPdfDocumentReader("classpath:/sample1.pdf",
PdfDocumentReaderConfig.builder()
.withPageTopMargin(0)
.withPageBottomMargin(0)
.withPageExtractedTextFormatter(ExtractedTextFormatter.builder()
.withNumberOfTopTextLinesToDelete(0)
.withNumberOfBottomTextLinesToDelete(3)
.withNumberOfTopPagesToSkipBeforeDelete(0)
.build())
.withPagesPerDocument(1)
.build());
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"Document outline (e.g. TOC) is null. Make sure the PDF document has a table of contents (TOC). If not, consider the PagePdfDocumentReader or the TikaDocumentReader instead.");
}
}