PyPDFToDocument
A component that converts PDF files to Documents.
Name | PyPDFToDocument |
Folder Path | /converters/ |
Position in a Pipeline | Before PreProcessors, or right at the beginning of an indexing Pipeline |
Inputs | "sources": PDF file paths or ByteStream objects |
Outputs | "documents": a list of Documents |
Overview
The PyPDFToDocument
component converts PDF files into Documents. You can use it in an indexing Pipeline to index the contents of a PDF file into a Document Store. It takes a list of file paths or ByteStream objects as input and outputs the converted result as a list of Documents. Optionally, you can attach metadata to the Documents through the meta
input parameter.
The component uses a default text extraction converter if you don't provide any custom converter through the converter_name
parameter when initializing the component.
Usage
You need to install pypdf
package to use the PyPDFToDocument
converter:
pip install pypdf
On its own
from haystack.components.converters import PyPDFToDocument
converter = PyPDFToDocument()
docs = converter.run(sources=[Path("my_file.pdf")])
In a Pipeline
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.converters import PyPDFToDocument
from haystack.components.preprocessors import DocumentCleaner
from haystack.components.preprocessors import DocumentSplitter
from haystack.components.writers import DocumentWriter
document_store = InMemoryDocumentStore()
pipeline = Pipeline()
pipeline.add_component("converter", PyPDFToDocument())
pipeline.add_component("cleaner", DocumentCleaner())
pipeline.add_component("splitter", DocumentSplitter(split_by="sentence", split_length=5))
pipeline.add_component("writer", DocumentWriter(document_store=document_store))
pipeline.connect("converter", "cleaner")
pipeline.connect("cleaner", "splitter")
pipeline.connect("splitter", "writer")
pipeline.run({"converter": {"sources": file_names}})
Updated 8 months ago
Related Links
See the parameters details in our API reference: