LLM fragment plugin to load a PDF as a sequence of images
Install this plugin in the same environment as LLM.
llm install llm-pdf-to-images
The llm-pdf-to-images
plugin provides a fragment loader that converts each page of a PDF document into an image attachment.
You can use the pdf-to-images:
fragment prefix to convert a PDF file into a series of image attachments which can be sent to a model.
Example usage:
llm -f pdf-to-images:path/to/document.pdf 'Summarize this document'
pdf-to-images:<path>?dpi=N&format=jpg|png&quality=Q
<path>
: Path to the PDF file accessible to the environment where LLM runs.dpi=N
: (optional) Dots per inch to use when rendering the PDF pages, which affects the resolution of the output images. Defaults to300
if omitted.format=jpg|png
: (optional) Image format to use for the output. Can be eitherjpg
(default) orpng
.quality=Q
: (optional) JPEG quality factor between 1 and 100. Only applies when using JPG format. Defaults to30
if omitted. Higher values produce better quality but larger file sizes.
Convert a PDF file to images with default settings (300 DPI, JPG format, quality 30):
llm -f pdf-to-images:document.pdf 'summarize this document'
Convert a PDF with higher resolution (600 DPI):
llm -f 'pdf-to-images:document.pdf?dpi=600' 'summarize'
Convert a PDF to PNG format:
llm -f 'pdf-to-images:document.pdf?format=png' 'describe all figures'
Convert a PDF with high-quality JPG images:
llm -f 'pdf-to-images:document.pdf?quality=90' 'extract all visible text'
Combine multiple parameters:
llm -f 'pdf-to-images:document.pdf?dpi=450&format=jpg&quality=75' 'OCR'
To set up this plugin locally, first checkout the code. Then create a new virtual environment:
cd llm-pdf-to-images
python -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
python -m pip install -e '.[test]'
To run the tests:
python -m pytest