Getting Nougat to Play Nice with PDFs (The Manual Patch Method)
The nougat package has a pesky bug in how it handles PDFs. Don’t worry, it’s an easy fix! Here’s how to install it from the source and apply a manual patch, based on this pull request.
First, Make a Home for Your Project
mkdir -p /path/to/nougat cd /path/to/nougatWhip Up a Virtual Environment This keeps all the project’s Python bits neatly tucked away from the rest of your system.
python3 -m venv .venv source .venv/bin/activate(If you’re a
pyenvuser, you can set a local Python version before you start.)Install Nougat Straight from GitHub
pip3 install git+https://github.com/facebookresearch/nougatHeads up: This can take a few minutes, especially on an older machine. Go grab a coffee!
Time for a Little Open-Source Surgery Alright, here comes the magic. We need to manually edit one of Nougat’s files to fix the PDF rendering.
Hunt down the file: Find and open `rasterize.py` inside your virtual environment. The path will look something like this:
.venv/lib/pythonX.Y/site-packages/nougat/utils/rasterize.pyFind this chunk of code:
# As pypdfium2 does not render pages at the document level rather it is done at the page level renderer = pdf.render( pypdfium2.PdfBitmap.to_pil, page_indices=pages, scale=dpi / 72, ) for i, image in zip(pages, renderer):Swap it out with this better version:
images = [] for idx in pages: page = pdf.get_page(idx) pil_image = page.render( scale=dpi / 72, rotation=0, ).to_pil() images.append(pil_image) page.close() for i, image in zip(pages, images):
Take It for a Spin! With our patch in place, you’re ready to go.
To run it while your venv is active:
nougat --helpTo run it from anywhere, just use the full path:
/path/to/nougat/.venv/bin/nougat --helpAnd when you’re done, you can leave the virtual environment:
deactivate
Full disclosure: I’ve actually switched to using marker for my own work. I find it way more powerful than nougat, and the best part is how lightweight and fast it is—it runs like a dream even on a CPU-only machine.