#### NOTE
This is a **community plugin**, an external project maintained by its respective author.
Community plugins are not part of FiftyOne core and may change independently.
Please review each plugin’s documentation and license before use.

<a href="https://github.com/brimoor/pdf-loader" target="_blank">![GitHub Repo](https://img.shields.io/badge/GitHub-Repository-black?logo=github)</a>

# PDF Loader Plugin

A [FiftyOne plugin](https://docs.voxel51.com/plugins/index.html) for loading
PDFs as images.

<video controls width="100%" style="max-width: 600px; height: auto;"><source src="https://github.com/brimoor/pdf-loader/assets/25985824/584bd14f-e076-4b88-89d5-f88190032f93" type="video/mp4"></video>

## Installation

If you haven’t already,
[install FiftyOne](https://docs.voxel51.com/getting_started/install.html):

```shell
pip install fiftyone
```

Then install the plugin and its dependencies:

```shell
fiftyone plugins download https://github.com/brimoor/pdf-loader

brew install poppler
pip install pdf2image
```

## Usage

### Using the App UI

1. Launch the App:

```py
import fiftyone as fo

dataset = fo.Dataset()
session = fo.launch_app(dataset)
```

1. Press ``` or click the `Browse operations` icon above the grid
2. Run the `pdf_loader` operator

### Using the SDK

You can use the plugin programmatically from Python:

```python
import fiftyone as fo
import fiftyone.operators as foo

import requests
import os

# Download a PDF from a URL (optional - you can use any local PDF)
url = "https://arxiv.org/pdf/2309.11419"
filename = url.split('/')[-1] + ".pdf"  # Add .pdf extension

response = requests.get(url)

if response.status_code == 200:
    with open(filename, 'wb') as f:
        f.write(response.content)
    print(f"Downloaded {filename}")
else:
    print(f"Failed to download {filename}. Status code: {response.status_code}")

# Load the PDF loader operator
pdf_loader = foo.get_operator("@brimoor/pdf-loader/pdf_loader")

# Create a dataset for the PDF pages
pdf_dataset = fo.Dataset("pdf_dataset")

# Convert PDF to images and add to dataset
pdf_loader(
    pdf_dataset,
    input_path="./2309.11419.pdf",  # Path to your PDF file
    output_dir="./pdf_images",     # Directory to save the images
    dpi=200,                        # Image quality in DPI
    fmt="png",                      # Image format (png or jpg)
    tags=None,                      # Optional tags for samples
    delegate=False                  # Set to True for async execution
)
```

## What next?

Install the
[PyTesseract OCR](https://github.com/jacobmarks/pytesseract-ocr-plugin) and
[Semantic Document Search](https://github.com/jacobmarks/semantic-document-search-plugin)
plugins to make your documents searchable!

<video controls width="100%" style="max-width: 600px; height: auto;"><source src="https://github.com/brimoor/pdf-loader/assets/25985824/e18fde7f-eced-41dc-849a-a0e074a20737" type="video/mp4"></video>

1. Install the plugins and their dependencies:

```shell
fiftyone plugins download https://github.com/jacobmarks/pytesseract-ocr-plugin
pip install pytesseract

<video controls width="100%" style="max-width: 600px; height: auto;"><source src="https://github.com/jacobmarks/semantic-document-search-plugin
pip install qdrant_client
pip install sentence_transformers
```

1. Launch a Qdrant server:

```default
docker run -p "6333:6333" -p "6334:6334" -d qdrant/qdrant" type="video/mp4"></video>
```

1. Run the `run_ocr_engine` operator to detect text blocks
2. Run the `create_semantic_document_index` operator to generate a semantic
   index for the text blocks
3. Run the `semantically_search_documents` operator to perform arbitrary
   searches against the index!

## Implementation

This plugin is a basically a wrapper around the following code:

```py
import os
from pdf2image import convert_from_path

INPUT_PATH = "/path/to/your.pdf"
OUTPUT_DIR = "/path/for/page/images"

os.makedirs(OUTPUT_DIR, exist_ok=True)
convert_from_path(INPUT_PATH, output_folder=OUTPUT_DIR, fmt="jpg")

dataset.add_images_dir(OUTPUT_DIR)
```
