crawl4ai icon indicating copy to clipboard operation
crawl4ai copied to clipboard

[Bug]: PDF images saved with save_images_locally=True come back with an empty src in result.media

Open michaelkillgta opened this issue 1 month ago • 1 comments

crawl4ai version

0.9.5 (develop at 78405d6)

Expected Behavior

With PDFContentScrapingStrategy(extract_images=True, save_images_locally=True), each entry in result.media.images should point at the image file that was written to image_save_dir.

Current Behavior

The image is written to disk, but the MediaItem in result.media.images has src == "" and data == "", so there is no way to find the file from the result.

The PDF processor reports a saved image as {"path": ..., "format": ..., "width": ..., ...}. scrap() passes these dicts straight into ScrapingResult(media={"images": [...]}), and MediaItem has no path field, so pydantic drops it. Same kind of schema mismatch as #2374 for links. Inline images (save_images_locally=False) are fine because data is a MediaItem field.

Is this reproducible?

Yes

Inputs Causing the Bug

Any PDF with an embedded image, e.g. one made by Pillow (below).

Steps to Reproduce

1. Make a one-page PDF containing an image.
2. Scrape it with PDFContentScrapingStrategy(extract_images=True, save_images_locally=True, image_save_dir=...).
3. Look at result.media.images.

Code snippets

import os, tempfile
from PIL import Image
from crawl4ai.processors.pdf import PDFContentScrapingStrategy

d = tempfile.mkdtemp()
pdf = os.path.join(d, "doc.pdf")
Image.new("RGB", (40, 30), "red").save(pdf)

strategy = PDFContentScrapingStrategy(
    extract_images=True, save_images_locally=True, image_save_dir=os.path.join(d, "imgs")
)
result = strategy.scrap(pdf, "")
print(os.listdir(os.path.join(d, "imgs")))
print(result.media.images[0].model_dump())

Output:

['page_1_img_1.jpg']
{'src': '', 'data': '', 'alt': '', 'desc': '', 'score': 0, 'type': 'image', 'group_id': 0, 'format': 'jpeg', 'width': 40}

OS

Linux

Python version

3.12

Browser

N/A (PDF scraping strategy called directly)

Browser version

N/A

Error logs & Screenshots (if applicable)

No error; success is True.

michaelkillgta avatar Oct 29 '26 21:10 michaelkillgta

Fixed on develop by #2470. The saved image's path now comes back in result.media.images[i].src. We'll close this when the fix ships in the next release.

ntohidi avatar Nov 13 '26 10:11 ntohidi