Skip to content

fix(pdf): create output directory in convert_pdf_to_images#1290

Open
Osamaali313 wants to merge 1 commit into
anthropics:mainfrom
Osamaali313:fix/pdf-create-output-dir
Open

fix(pdf): create output directory in convert_pdf_to_images#1290
Osamaali313 wants to merge 1 commit into
anthropics:mainfrom
Osamaali313:fix/pdf-create-output-dir

Conversation

@Osamaali313

Copy link
Copy Markdown

Summary

Fixes #1025.

skills/pdf/scripts/convert_pdf_to_images.py writes each rendered page to
os.path.join(output_dir, f"page_{i+1}.png") but never creates output_dir.
If the directory does not already exist, image.save(...) raises
FileNotFoundError and no pages are written.

Fix

Create the output directory at the start of convert():

os.makedirs(output_dir, exist_ok=True)

exist_ok=True keeps the existing-directory case a no-op, so behaviour is
unchanged when the directory is already present.

Test

Adds skills/pdf/scripts/test_convert_pdf_to_images.py, a dependency-free
unittest (stubs pdf2image, so neither pdf2image nor poppler is required):
it converts into a not-yet-existing nested directory using a fake image whose
save() writes a real file, and asserts the directory and page_1.png /
page_2.png are created. The test fails before the fix (the original
FileNotFoundError) and passes after.

Run:

python -m unittest test_convert_pdf_to_images -v

convert() saved page_N.png into output_dir without ensuring the
directory existed, so passing a non-existent output directory crashed
with FileNotFoundError (anthropics#1025).

Create the output directory with os.makedirs(..., exist_ok=True) before
writing the pages. Add a unittest that converts with a mocked image into
a missing directory and asserts the directory and page files are created.
Copilot AI review requested due to automatic review settings June 8, 2026 18:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a safeguard to ensure convert_pdf_to_images.convert() always has a valid output directory, and introduces a unit test to verify directory creation and file output without requiring pdf2image/Poppler to be installed.

Changes:

  • Ensure output_dir is created before saving generated images.
  • Add a unittest validating that missing nested output directories are created and PNGs are written.
  • Stub pdf2image in tests to avoid external runtime dependencies.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
skills/pdf/scripts/convert_pdf_to_images.py Creates the output directory up-front to prevent failures when saving images.
skills/pdf/scripts/test_convert_pdf_to_images.py Adds an isolated unittest that stubs pdf2image and checks directory + output files creation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +12 to +17
if "pdf2image" not in sys.modules:
_stub = types.ModuleType("pdf2image")
_stub.convert_from_path = lambda *args, **kwargs: []
sys.modules["pdf2image"] = _stub

sys.path.insert(0, str(Path(__file__).resolve().parent))
Comment on lines 9 to 12
def convert(pdf_path, output_dir, max_dim=1000):
os.makedirs(output_dir, exist_ok=True)

images = convert_from_path(pdf_path, dpi=200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pdf skill: convert_pdf_to_images.py fails with FileNotFoundError if output directory doesn't exist

2 participants