PyPDFToDocument
Convert PDF files to documents your pipeline can query. This component uses the PyPDF library to extract text from PDFs and supports both plain and layout-aware extraction modes.
Key Features
- Converts PDF files to documents using the PyPDF library.
- Supports plain extraction mode and an experimental layout mode that adheres to the rendered PDF layout.
- Configurable orientation handling and space width in plain mode.
- Configurable vertical spacing and font height weight in layout mode.
- Accepts both file paths and ByteStream objects as input.
- Optionally stores the file name or the full file path in document metadata.
Configuration
- Drag the
PyPDFToDocumentcomponent onto the canvas from the Component Library. - Click the component to open the configuration panel.
- Configure the parameters as needed.
Connections
PyPDFToDocument accepts a list of file paths or ByteStream objects (sources) as input, along with optional metadata (meta). It outputs a list of converted documents (documents).
Typically, PyPDFToDocument receives PDF files routed from FileTypeRouter and sends its output to DocumentJoiner, which combines documents from multiple converters before passing them downstream for splitting and indexing.
Usage Example
Using the Component in an Index
In this index, PyPDFToDocument receives PDF files from FileTypeRouter and sends them to DocumentJoiner.
components:
FileTypeRouter:
type: haystack.components.routers.file_type_router.FileTypeRouter
init_parameters:
mime_types:
- text/plain
- application/pdf
- text/markdown
TextFileToDocument:
type: haystack.components.converters.txt.TextFileToDocument
init_parameters:
encoding: utf-8
store_full_path: false
PyPDFToDocument:
type: haystack.components.converters.pdf.PyPDFToDocument
init_parameters:
extraction_mode: PyPDFExtractionMode.PLAIN
plain_mode_orientations:
- 0
- 90
- 180
- 270
plain_mode_space_width: 200.0
layout_mode_space_vertically: true
layout_mode_scale_weight: 1.25
layout_mode_strip_rotated: true
layout_mode_font_height_weight: 1.0
store_full_path: false
MarkdownToDocument:
type: haystack.components.converters.markdown.MarkdownToDocument
init_parameters:
table_to_single_line: false
progress_bar: true
store_full_path: false
DocumentJoiner:
type: haystack.components.joiners.document_joiner.DocumentJoiner
init_parameters:
join_mode: concatenate
weights:
top_k:
sort_by_score: true
DocumentSplitter:
type: haystack.components.preprocessors.document_splitter.DocumentSplitter
init_parameters:
split_by: word
split_length: 200
split_overlap: 0
split_threshold: 0
splitting_function:
respect_sentence_boundary: false
language: en
use_split_rules: true
extend_abbreviations: true
DocumentWriter:
type: haystack.components.writers.document_writer.DocumentWriter
init_parameters:
policy: NONE
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: pdf-index
max_chunk_bytes: 104857600
embedding_dim: 768
return_embedding: false
method:
mappings:
settings:
create_index: true
http_auth:
use_ssl:
verify_certs:
timeout:
connections:
- sender: FileTypeRouter.text/plain
receiver: TextFileToDocument.sources
- sender: FileTypeRouter.application/pdf
receiver: PyPDFToDocument.sources
- sender: FileTypeRouter.text/markdown
receiver: MarkdownToDocument.sources
- sender: TextFileToDocument.documents
receiver: DocumentJoiner.documents
- sender: PyPDFToDocument.documents
receiver: DocumentJoiner.documents
- sender: MarkdownToDocument.documents
receiver: DocumentJoiner.documents
- sender: DocumentJoiner.documents
receiver: DocumentSplitter.documents
- sender: DocumentSplitter.documents
receiver: DocumentWriter.documents
max_runs_per_component: 100
Parameters
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| sources | List[Union[str, Path, ByteStream]] | List of file paths or ByteStream objects to convert. | |
| meta | Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] | None | Optional metadata to attach to the documents. This value can be a list of dictionaries or a single dictionary. If it's a single dictionary, its content is added to the metadata of all produced documents. If it's a list, its length must match the number of sources, as they are zipped together. For ByteStream objects, their meta is added to the output documents. |
Outputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| documents | List[Document] | A list of converted documents. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
| extraction_mode | Union[str, PyPDFExtractionMode] | PyPDFExtractionMode.PLAIN | The mode to use for extracting text from a PDF. Layout mode is an experimental mode that adheres to the rendered layout of the PDF. |
| plain_mode_orientations | tuple | (0, 90, 180, 270) | Tuple of orientations to look for when extracting text from a PDF in plain mode. Ignored if extraction_mode is PyPDFExtractionMode.LAYOUT. |
| plain_mode_space_width | float | 200.0 | Forces default space width if not extracted from font. Ignored if extraction_mode is PyPDFExtractionMode.LAYOUT. |
| layout_mode_space_vertically | bool | True | Whether to include blank lines inferred from y distance + font height. Ignored if extraction_mode is PyPDFExtractionMode.PLAIN. |
| layout_mode_scale_weight | float | 1.25 | Multiplier for string length when calculating weighted average character width. Ignored if extraction_mode is PyPDFExtractionMode.PLAIN. |
| layout_mode_strip_rotated | bool | True | Layout mode does not support rotated text. Set to False to include rotated text anyway. If rotated text is discovered, layout will be degraded and a warning will be logged. Ignored if extraction_mode is PyPDFExtractionMode.PLAIN. |
| layout_mode_font_height_weight | float | 1.0 | Multiplier for font height when calculating blank line height. Ignored if extraction_mode is PyPDFExtractionMode.PLAIN. |
| store_full_path | bool | False | If True, the full path of the file is stored in the metadata of the document. If False, only the file name is stored. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
| sources | List[Union[str, Path, ByteStream]] | List of file paths or ByteStream objects to convert. | |
| meta | Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] | None | Optional metadata to attach to the documents. This value can be a list of dictionaries or a single dictionary. If it's a single dictionary, its content is added to the metadata of all produced documents. If it's a list, its length must match the number of sources, as they are zipped together. For ByteStream objects, their meta is added to the output documents. |
Was this page helpful?