quivr/backend/modules/assistant/ito/utils/pdf_generator.py
Stan Girard b62297341f
feat(assistants): Add PDF generation functionality and nice emails (#2436)
This pull request adds PDF generation functionality and improves the
formatting of emails. It includes a new module for generating PDFs using
the fpdf2 library and updates the email templates to use markdown
formatting for better readability.

<!--
ELLIPSIS_HIDDEN
-->
----

| <a href="https://ellipsis.dev" target="_blank"><img
src="https://avatars.githubusercontent.com/u/80834858?s=400&u=31e596315b0d8f7465b3ee670f25cea677299c96&v=4"
alt="Ellipsis" width="30px" height="30px"/></a> | 🚀 This PR
description was created by [Ellipsis](https://www.ellipsis.dev) for
commit ccecff77a5. |
|--------|--------|

### Summary:
This PR introduces PDF generation functionality, improves email
formatting, sanitizes filenames, updates various files and dependencies,
and updates `docker-compose.dev.yml`.

**Key points**:
- Added PDF generation functionality using the `fpdf2` library
- Improved email formatting
- Sanitized filenames using the `unidecode` library
- Updated `backend/modules/assistant/ito/ito.py` to generate PDFs and
sanitize filenames
- Added `pdf_generator.py` in `backend/modules/assistant/ito/utils/` for
PDF generation
- Updated `Pipfile` and `Pipfile.lock` in
`backend/modules/assistant/ito/utils/` to include `unidecode`
- Updated `requirements.txt` with new dependencies
- Updated `docker-compose.dev.yml`


----
Generated with ❤️ by [ellipsis.dev](https://www.ellipsis.dev)

<!--
ELLIPSIS_HIDDEN
-->
2024-04-17 06:00:31 -07:00

91 lines
3.2 KiB
Python

import os
from fpdf import FPDF
from pydantic import BaseModel
class PDFModel(BaseModel):
title: str
content: str
class PDFGenerator(FPDF):
def __init__(self, pdf_model: PDFModel, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pdf_model = pdf_model
self.add_font(
"DejaVu",
"",
os.path.join(os.path.dirname(__file__), "font/DejaVuSansCondensed.ttf"),
uni=True,
)
self.add_font(
"DejaVu",
"B",
os.path.join(
os.path.dirname(__file__), "font/DejaVuSansCondensed-Bold.ttf"
),
uni=True,
)
self.add_font(
"DejaVu",
"I",
os.path.join(
os.path.dirname(__file__), "font/DejaVuSansCondensed-Oblique.ttf"
),
)
def header(self):
# Logo
logo_path = os.path.join(os.path.dirname(__file__), "logo.png")
self.image(logo_path, 10, 10, 20) # Adjust size as needed
# Move cursor to right of image
self.set_xy(20, 15)
# Title
self.set_font("DejaVu", "B", 12)
self.multi_cell(0, 10, self.pdf_model.title, align="C")
self.ln(5) # Padding after title
def footer(self):
self.set_y(-15)
self.set_font("DejaVu", "I", 8)
self.set_text_color(169, 169, 169)
self.cell(80, 10, "Generated by Quivr", 0, 0, "C")
self.set_font("DejaVu", "U", 8)
self.set_text_color(0, 0, 255)
self.cell(30, 10, "quivr.app", 0, 0, "C", link="https://quivr.app")
self.cell(0, 10, "Github", 0, 1, "C", link="https://github.com/quivrhq/quivr")
def chapter_body(self):
self.set_font("DejaVu", "", 12)
self.multi_cell(0, 10, self.pdf_model.content, markdown=True)
self.ln()
def print_pdf(self):
self.add_page()
self.chapter_body()
if __name__ == "__main__":
pdf_model = PDFModel(
title="Summary of Legal Services Rendered by Orrick",
content="""
**Summary:**
The document is an invoice from Quivr Technologies, Inc. for legal services provided to client YC W24, related to initial corporate work. The total fees and disbursements amount to $8,345.00 for services rendered through February 29, 2024. The invoice includes specific instructions for payment remittance and contact information for inquiries. Online payment through e-billexpress.com is also an option.
**Key Points:**
- Quivr Technologies, Inc., based in France and represented by Stanislas Girard, provided legal services to client YC W24.
- Services included preparing and completing forms, drafting instructions, reviewing and responding to emails, filing 83(b) elections, and finalizing documents for submission to YC.
- The timekeepers involved in providing these services were Julien Barbey, Maria T. Coladonato, Michael LaBlanc, Jessy K. Parker, Marisol Sandoval Villasenor, Alexis A. Smith, and Serena Tibrewala.
- The total hours billed for the services provided was 16.20, with a total cost of $8,345.00.
- Instructions for payment remittance, contact information, and online payment options through e-billex
""",
)
pdf = PDFGenerator(pdf_model)
pdf.print_pdf()
pdf.output("simple.pdf")