Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package org.springframework.ai.reader.pdf.config;

import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
Expand All @@ -41,6 +42,8 @@
*/
public class ParagraphManager {

private static final Log logger = LogFactory.getLog(ParagraphManager.class);

/**
* Root of the paragraphs tree.
*/
Expand All @@ -64,7 +67,9 @@ public ParagraphManager(PDDocument document) {
new Paragraph(null, "root", -1, 1, this.document.getNumberOfPages(), 0),
this.document.getDocumentCatalog().getDocumentOutline(), 0);

printParagraph(this.rootParagraph, System.out);
if (logger.isDebugEnabled()) {
logParagraph(this.rootParagraph);
}
}
catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -87,10 +92,10 @@ private void flatten(Paragraph current, List<Paragraph> paragraphs) {
}
}

private void printParagraph(Paragraph paragraph, PrintStream printStream) {
printStream.println(paragraph);
private void logParagraph(Paragraph paragraph) {
logger.debug(paragraph);
for (Paragraph childParagraph : paragraph.children()) {
printParagraph(childParagraph, printStream);
logParagraph(childParagraph);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;

import org.apache.pdfbox.Loader;
Expand Down Expand Up @@ -114,4 +115,18 @@ void shouldSkipInvalidOutline() throws IOException {
assertThat(documents.get(1).getMetadata().get("title")).isEqualTo("Chapter 3");
}

@Test
void shouldNotWriteToStdoutDuringInitialization() {
PrintStream originalOut = System.out;
ByteArrayOutputStream capturedOutput = new ByteArrayOutputStream();
System.setOut(new PrintStream(capturedOutput));
try {
new ParagraphPdfDocumentReader("classpath:/sample3.pdf", PdfDocumentReaderConfig.defaultConfig());
}
finally {
System.setOut(originalOut);
}
assertThat(capturedOutput.toString()).isEmpty();
}

}