[PR #30399] feat: support image extraction in PDF RAG extractor #32800

Closed
opened 2026-02-21 20:52:06 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/30399

State: closed
Merged: Yes


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #30398

Description: This PR introduces image extraction capabilities to the PDF processing workflow. When a PDF is indexed, the extractor now scans for images, saves them to storage, and embeds them into the resulting document content as Markdown links.

Changes:

  • PdfExtractor Enhancement:
    • Added _extract_images method using pypdfium2.raw to access low-level PDF image objects.
    • Implemented image format detection via magic bytes.
    • Integrated with extensions.ext_storage and UploadFile model to persist images.
  • ExtractProcessor Update:
    -Updated to pass tenant_id and user_id context to the PdfExtractor.
  • Testing:
    • Added comprehensive unit tests in api/tests/unit_tests/core/rag/extractor/test_pdf_extractor.py covering various image formats and edge cases.

Type of Change:

  • New feature (non-breaking change which adds functionality)
  • Unit tests added

How has this been tested?

  • Executed pytest api/tests/unit_tests/core/rag/extractor/test_pdf_extractor.py.
  • Verified image links are correctly generated and point to the valid file preview endpoint.
  • Confirmed that text extraction remains unaffected and works in tandem with image extraction.

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/30399 **State:** closed **Merged:** Yes --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 1. Ensure there is an associated issue and you have been assigned to it > 1. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary <!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes #30398 **Description:** This PR introduces image extraction capabilities to the PDF processing workflow. When a PDF is indexed, the extractor now scans for images, saves them to storage, and embeds them into the resulting document content as Markdown links. **Changes:** - `PdfExtractor` Enhancement: - Added `_extract_images` method using `pypdfium2.raw` to access low-level PDF image objects. - Implemented image format detection via magic bytes. - Integrated with `extensions.ext_storage` and `UploadFile` model to persist images. - `ExtractProcessor` Update: -Updated to pass `tenant_id` and `user_id` context to the `PdfExtractor`. - Testing: - Added comprehensive unit tests in `api/tests/unit_tests/core/rag/extractor/test_pdf_extractor.py` covering various image formats and edge cases. Type of Change: - [x] New feature (non-breaking change which adds functionality) - [x] Unit tests added How has this been tested? - Executed `pytest api/tests/unit_tests/core/rag/extractor/test_pdf_extractor.py`. - Verified image links are correctly generated and point to the valid file preview endpoint. - Confirmed that text extraction remains unaffected and works in tandem with image extraction. ## Screenshots | Before | After | |--------|-------| | ... | ... | ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:52:06 -05:00
yindo closed this issue 2026-02-21 20:52:06 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32800