@ -128,18 +128,36 @@ class PyPDFParser(BaseBlobParser):
class PDFMinerParser ( BaseBlobParser ) :
""" Parse `PDF` using `PDFMiner`. """
def __init__ ( self , extract_images : bool = False ) :
def __init__ ( self , extract_images : bool = False , * , concatenate_pages : bool = True ) :
""" Initialize a parser based on PDFMiner.
Args :
extract_images : Whether to extract images from PDF .
concatenate_pages : If True , concatenate all PDF pages into one a single
document . Otherwise , return one document per page .
"""
self . extract_images = extract_images
self . concatenate_pages = concatenate_pages
def lazy_parse ( self , blob : Blob ) - > Iterator [ Document ] :
""" Lazily parse the blob. """
if not self . extract_images :
from pdfminer . high_level import extract_text
with blob . as_bytes_io ( ) as pdf_file_obj :
text = extract_text ( pdf_file_obj )
metadata = { " source " : blob . source }
yield Document ( page_content = text , metadata = metadata )
if self . concatenate_pages :
text = extract_text ( pdf_file_obj )
metadata = { " source " : blob . source }
yield Document ( page_content = text , metadata = metadata )
else :
from pdfminer . pdfpage import PDFPage
pages = PDFPage . get_pages ( pdf_file_obj )
for i , _ in enumerate ( pages ) :
text = extract_text ( pdf_file_obj , page_numbers = [ i ] )
metadata = { " source " : blob . source , " page " : str ( i ) }
yield Document ( page_content = text , metadata = metadata )
else :
import io