Contents

Getting Nougat to Play Nice with PDFs (The Manual Patch Method)

Contents

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.

  1. First, Make a Home for Your Project

    mkdir -p /path/to/nougat
    cd /path/to/nougat
    
  2. Whip 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 pyenv user, you can set a local Python version before you start.)

  3. Install Nougat Straight from GitHub

    pip3 install git+https://github.com/facebookresearch/nougat
    

    Heads up: This can take a few minutes, especially on an older machine. Go grab a coffee!

  4. 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.py

    • Find 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):
      
  5. Take It for a Spin! With our patch in place, you’re ready to go.

    • To run it while your venv is active:

      nougat --help
      
    • To run it from anywhere, just use the full path:

      /path/to/nougat/.venv/bin/nougat --help
      
    • And 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.