1

Used 3 processors to do this

  1. GetFile
  2. ExecuteScript
  3. PutFile

In the Execute Script processor-- Used groovy script and followed the steps in this link below. It works fine but the last few pages / last few lines of the last page does not get extracted. Tried it with different Pdf files and ran into same issue.

   import org.apache.pdfbox.pdmodel.*
import org.apache.pdfbox.util.*

def flowFile = session.get()
if(!flowFile) return

def doc, info
def s  = new PDFTextStripper()

flowFile = session.write(flowFile, {inputStream, outputStream ->
 doc = PDDocument.load(inputStream)
 info = doc.getDocumentInformation()

        s.writeText(doc, new OutputStreamWriter(outputStream))
    } as StreamCallback
)
flowFile = session.putAttribute(flowFile, 'pdf.page.count', "${doc.getNumberOfPages()}")
flowFile = session.putAttribute(flowFile, 'pdf.title', "${info.getTitle()}" )
flowFile = session.putAttribute(flowFile, 'pdf.author',"${info.getAuthor()}" );
flowFile = session.putAttribute(flowFile, 'pdf.subject', "${info.getSubject()}" );
flowFile = session.putAttribute(flowFile, 'pdf.keywords', "${info.getKeywords()}" );
flowFile = session.putAttribute(flowFile, 'pdf.creator', "${info.getCreator()}" );
flowFile = session.putAttribute(flowFile, 'pdf.producer', "${info.getProducer()}" );
flowFile = session.putAttribute(flowFile, 'pdf.date.creation', "${info.getCreationDate()}" );
flowFile = session.putAttribute(flowFile, 'pdf.date.modified', "${info.getModificationDate()}");
flowFile = session.putAttribute(flowFile, 'pdf.trapped', "${info.getTrapped()}" );   
session.transfer(flowFile, REL_SUCCESS)

http://funnifi.blogspot.com/2016/02/executescript-extract-text-metadata.html

Is there a way to fix this?

4
  • The problem could be in a document itself. For example the text could be in an image. And there is no writer.flush() that could be a reason of the problem. Please share your code in the question.
    – daggett
    Mar 15, 2019 at 5:31
  • where should I use Write.flush? The pdfs I tried did not have images in them.
    – user4321
    Mar 15, 2019 at 14:54
  • 1
    Try this: outputStream.withWriter{w-> s.writeText(doc, w) } instead of corresponding line in your code
    – daggett
    Mar 16, 2019 at 3:36
  • @daggett great. That did the miracle. Can you please post this as an answer?
    – user4321
    Mar 18, 2019 at 13:34

2 Answers 2

1

the problem seems in this line of code:

s.writeText(doc, new OutputStreamWriter(outputStream))

you are creating the OutputStreamWriter that internally has a buffer that transferred to underlying output stream on call of OutputStreamWriter.flush() or OutputStreamWriter.close(). None of those methods called in your code.

You can use groovy method withWriter to close writer after closure finished:

outputStream.withWriter{w-> s.writeText(doc, w) }
0

I reproduced your code but I received the following error :

unable to resolve class PDFTextStripper(). This is jars file in path of module directory:
jempbox-1.8.12.jar
fontbox-1.8.12.jar
commons-logging-1.1.1.jar 

It looks to me that there are missing dependence.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.