[PR #6156] [MERGED] Feat: Enhance PDF Downloader to Export Chat #8628

Closed
opened 2025-11-11 18:01:28 -06:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/6156
Author: @noczero
Created: 10/13/2024
Status: Merged
Merged: 10/13/2024
Merged by: @tjbck

Base: devHead: add-pdf-generator


📝 Commits (1)

  • c41261e feat: PDF generator to export chat

📊 Changes

11 files changed (+428 additions, -56 deletions)

View changed files

📝 backend/open_webui/apps/webui/models/chats.py (+3 -0)
📝 backend/open_webui/apps/webui/routers/utils.py (+6 -56)
backend/open_webui/static/assets/pdf-style.css (+283 -0)
backend/open_webui/static/fonts/NotoSans-Bold.ttf (+0 -0)
backend/open_webui/static/fonts/NotoSans-Italic.ttf (+0 -0)
backend/open_webui/static/fonts/NotoSans-Regular.ttf (+0 -0)
backend/open_webui/static/fonts/NotoSansJP-Regular.ttf (+0 -0)
backend/open_webui/static/fonts/NotoSansKR-Regular.ttf (+0 -0)
backend/open_webui/static/fonts/NotoSansSC-Regular.ttf (+0 -0)
backend/open_webui/utils/pdf_generator.py (+134 -0)
📝 backend/requirements.txt (+2 -0)

📄 Description

Pull Request Checklist

Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.

Before submitting, make sure you've checked the following:

  • Target branch: Please verify that the pull request targets the dev branch.
  • Description: Provide a concise description of the changes made in this pull request.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Documentation: Have you updated relevant documentation Open WebUI Docs, or other documentation sources?
  • Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
  • Testing: Have you written and run sufficient tests for validating the changes?
  • Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
  • Prefix: To cleary categorize this pull request, prefix the pull request title, using one of the following:
    • BREAKING CHANGE: Significant changes that may affect compatibility
    • build: Changes that affect the build system or external dependencies
    • ci: Changes to our continuous integration processes or workflows
    • chore: Refactor, cleanup, or other non-functional code changes
    • docs: Documentation update or addition
    • feat: Introduces a new feature or enhancement to the codebase
    • fix: Bug fix or error correction
    • i18n: Internationalization or localization changes
    • perf: Performance improvement
    • refactor: Code restructuring for better maintainability, readability, or scalability
    • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)
    • test: Adding missing tests or correcting existing tests
    • WIP: Work in progress, a temporary label for incomplete or ongoing work

Changelog Entry

Description

This pull request introduces a new feature that enhances the PDF export functionality in the backend.

The enhancement includes the ability to render markdown-formatted chat messages in the exported PDF. '

This change addresses the limitation where markdown elements such as tables, headings, lists, and styles were previously rendered as raw text in the PDF.

Added

  • A new PDFGenerator class in backend/open_webui/utils/pdf_generator.py that handles the conversion of markdown-formatted chat messages into a styled PDF.
  • A CSS stylesheet pdf-style.css located at backend/open_webui/static/assets/pdf-style.css to define styles for various markdown elements when rendering them in the PDF.
  • New dependencies added to backend/requirements.txt:
    • xhtml2pdf==0.2.16 for converting HTML to PDF.
    • pymdown-extensions==10.11.2 for enhanced markdown processing.

Changed

  • Refactored the PDF generation logic in backend/open_webui/apps/webui/routers/utils.py to utilize the new PDFGenerator class for creating PDFs from chat messages.
  • UpdatedChatTitleMessagesForm schema object which invoke in PDFGenerator and utils route.

Deprecated

  • None

Removed

  • Removed direct font file dependencies from the project, specifically:
    • NotoSans-Bold.ttf
    • NotoSans-Italic.ttf
    • NotoSans-Regular.ttf
    • NotoSansJP-Regular.ttf
    • NotoSansKR-Regular.ttf
    • NotoSansSC-Regular.ttf

Fixed

  • None.

Security

  • None.

Breaking Changes

  • BREAKING CHANGE: The direct use of font files for PDF generation has been removed. This change may affect environments that relied on these specific font files being present in the project.

Additional Information

  • This enhancement provides a more polished and professional appearance for exported chat PDFs by supporting markdown syntax, which is commonly used in chat messages for formatting.
  • The xhtml2pdf library was chosen despite known issues with list rendering, as it provides a robust solution for HTML to PDF conversion. Future updates may address these issues as the library evolves.
  • The use of pymdown-extensions allows for extended markdown capabilities, making the chat export feature more versatile and powerful.
  • Also, I experimented with pdfkit and weasyprint as alternatives. Although pdfkit produces the best output,
    it requires an additional step to install the wkhtmltopdf binary package, beyond just a simple pip install.

Screenshots or Videos

  • Before
    Screenshot 2024-10-13 at 11 46 41

  • After
    Screenshot 2024-10-13 at 11 50 08


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/6156 **Author:** [@noczero](https://github.com/noczero) **Created:** 10/13/2024 **Status:** ✅ Merged **Merged:** 10/13/2024 **Merged by:** [@tjbck](https://github.com/tjbck) **Base:** `dev` ← **Head:** `add-pdf-generator` --- ### 📝 Commits (1) - [`c41261e`](https://github.com/open-webui/open-webui/commit/c41261e72b94a6e69ffa3ec00b84394dd9bab1d1) feat: PDF generator to export chat ### 📊 Changes **11 files changed** (+428 additions, -56 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/apps/webui/models/chats.py` (+3 -0) 📝 `backend/open_webui/apps/webui/routers/utils.py` (+6 -56) ➕ `backend/open_webui/static/assets/pdf-style.css` (+283 -0) ➖ `backend/open_webui/static/fonts/NotoSans-Bold.ttf` (+0 -0) ➖ `backend/open_webui/static/fonts/NotoSans-Italic.ttf` (+0 -0) ➖ `backend/open_webui/static/fonts/NotoSans-Regular.ttf` (+0 -0) ➖ `backend/open_webui/static/fonts/NotoSansJP-Regular.ttf` (+0 -0) ➖ `backend/open_webui/static/fonts/NotoSansKR-Regular.ttf` (+0 -0) ➖ `backend/open_webui/static/fonts/NotoSansSC-Regular.ttf` (+0 -0) ➕ `backend/open_webui/utils/pdf_generator.py` (+134 -0) 📝 `backend/requirements.txt` (+2 -0) </details> ### 📄 Description # Pull Request Checklist ### Note to first-time contributors: Please open a discussion post in [Discussions](https://github.com/open-webui/open-webui/discussions) and describe your changes before submitting a pull request. **Before submitting, make sure you've checked the following:** - [x] **Target branch:** Please verify that the pull request targets the `dev` branch. - [x] **Description:** Provide a concise description of the changes made in this pull request. - [x] **Changelog:** Ensure a changelog entry following the format of [Keep a Changelog](https://keepachangelog.com/) is added at the bottom of the PR description. - [x] **Documentation:** Have you updated relevant documentation [Open WebUI Docs](https://github.com/open-webui/docs), or other documentation sources? - [x] **Dependencies:** Are there any new dependencies? Have you updated the dependency versions in the documentation? - [ ] **Testing:** Have you written and run sufficient tests for validating the changes? - [x] **Code review:** Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards? - [x] **Prefix:** To cleary categorize this pull request, prefix the pull request title, using one of the following: - **BREAKING CHANGE**: Significant changes that may affect compatibility - **build**: Changes that affect the build system or external dependencies - **ci**: Changes to our continuous integration processes or workflows - **chore**: Refactor, cleanup, or other non-functional code changes - **docs**: Documentation update or addition - **feat**: Introduces a new feature or enhancement to the codebase - **fix**: Bug fix or error correction - **i18n**: Internationalization or localization changes - **perf**: Performance improvement - **refactor**: Code restructuring for better maintainability, readability, or scalability - **style**: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.) - **test**: Adding missing tests or correcting existing tests - **WIP**: Work in progress, a temporary label for incomplete or ongoing work # Changelog Entry ### Description This pull request introduces a new feature that enhances the PDF export functionality in the backend. The enhancement includes the ability to render markdown-formatted chat messages in the exported PDF. ' This change addresses the limitation where markdown elements such as tables, headings, lists, and styles were previously rendered as raw text in the PDF. ### Added - A new `PDFGenerator` class in `backend/open_webui/utils/pdf_generator.py` that handles the conversion of markdown-formatted chat messages into a styled PDF. - A CSS stylesheet `pdf-style.css` located at `backend/open_webui/static/assets/pdf-style.css` to define styles for various markdown elements when rendering them in the PDF. - New dependencies added to `backend/requirements.txt`: - `xhtml2pdf==0.2.16` for converting HTML to PDF. - `pymdown-extensions==10.11.2` for enhanced markdown processing. ### Changed - Refactored the PDF generation logic in `backend/open_webui/apps/webui/routers/utils.py` to utilize the new `PDFGenerator` class for creating PDFs from chat messages. - Updated`ChatTitleMessagesForm` schema object which invoke in `PDFGenerator` and `utils` route. ### Deprecated - None ### Removed - Removed direct font file dependencies from the project, specifically: - `NotoSans-Bold.ttf` - `NotoSans-Italic.ttf` - `NotoSans-Regular.ttf` - `NotoSansJP-Regular.ttf` - `NotoSansKR-Regular.ttf` - `NotoSansSC-Regular.ttf` ### Fixed - None. ### Security - None. ### Breaking Changes - **BREAKING CHANGE**: The direct use of font files for PDF generation has been removed. This change may affect environments that relied on these specific font files being present in the project. --- ### Additional Information - This enhancement provides a more polished and professional appearance for exported chat PDFs by supporting markdown syntax, which is commonly used in chat messages for formatting. - The `xhtml2pdf` library was chosen despite known issues with list rendering, as it provides a robust solution for HTML to PDF conversion. Future updates may address these issues as the library evolves. - The use of `pymdown-extensions` allows for extended markdown capabilities, making the chat export feature more versatile and powerful. - Also, I experimented with pdfkit and weasyprint as alternatives. Although pdfkit produces the best output, it requires an additional step to install the wkhtmltopdf binary package, beyond just a simple pip install. ### Screenshots or Videos - Before <img width="1872" alt="Screenshot 2024-10-13 at 11 46 41" src="https://github.com/user-attachments/assets/4b400f8e-d1a3-4ee6-9642-17bdcdb60e6c"> - After <img width="1872" alt="Screenshot 2024-10-13 at 11 50 08" src="https://github.com/user-attachments/assets/ccbc508a-a626-4b7e-8f56-cec648877ba6"> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2025-11-11 18:01:28 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#8628