Spaces:
Sleeping
Sleeping
Force ASCII encoding for HF PDF stability
Browse files- vault_logic.py +23 -27
vault_logic.py
CHANGED
|
@@ -145,37 +145,33 @@ def ai_summarize_text(text):
|
|
| 145 |
# The Logic Functions : These handle the actual file creation.
|
| 146 |
def create_pdf(title, content):
|
| 147 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
pdf = FPDF()
|
| 149 |
pdf.add_page()
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
if os.path.exists(font_path):
|
| 156 |
-
pdf.add_font('DejaVu', '', font_path, uni=True)
|
| 157 |
-
pdf.set_font('DejaVu', '', 14)
|
| 158 |
-
else:
|
| 159 |
-
# This is where it was falling back before, causing the "?" symbols
|
| 160 |
-
pdf.set_font("Arial", "B", 16)
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
# Write Title
|
| 164 |
-
pdf.multi_cell(0, 10, title)
|
| 165 |
pdf.ln(5)
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
return
|
|
|
|
| 177 |
except Exception as e:
|
| 178 |
-
|
|
|
|
| 179 |
|
| 180 |
def create_docx(title, content):
|
| 181 |
doc = Document()
|
|
|
|
| 145 |
# The Logic Functions : These handle the actual file creation.
|
| 146 |
def create_pdf(title, content):
|
| 147 |
try:
|
| 148 |
+
# 1. THE CLEANER: This removes long dashes, emojis, and special bullets
|
| 149 |
+
# It replaces them with nothing or a standard space so the PDF doesn't crash
|
| 150 |
+
clean_title = title.encode("ascii", "ignore").decode("ascii")
|
| 151 |
+
clean_content = content.encode("ascii", "ignore").decode("ascii")
|
| 152 |
+
|
| 153 |
pdf = FPDF()
|
| 154 |
pdf.add_page()
|
| 155 |
+
|
| 156 |
+
# 2. Use 'Arial' (the most stable font for Linux servers)
|
| 157 |
+
pdf.set_font("Arial", "B", 16)
|
| 158 |
+
pdf.multi_cell(0, 10, clean_title)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
pdf.ln(5)
|
| 160 |
+
|
| 161 |
+
pdf.set_font("Arial", size=12)
|
| 162 |
+
pdf.multi_cell(0, 10, clean_content)
|
| 163 |
+
|
| 164 |
+
# 3. Stream output as a string first
|
| 165 |
+
pdf_str = pdf.output(dest='S')
|
| 166 |
+
|
| 167 |
+
# 4. Convert to BYTES (Streamlit buttons on HF MUST have bytes)
|
| 168 |
+
if isinstance(pdf_str, str):
|
| 169 |
+
return pdf_str.encode('latin-1')
|
| 170 |
+
return pdf_str
|
| 171 |
+
|
| 172 |
except Exception as e:
|
| 173 |
+
# If there is still an error, this will show the message instead of crashing
|
| 174 |
+
return f"ERROR: {str(e)}".encode('utf-8')
|
| 175 |
|
| 176 |
def create_docx(title, content):
|
| 177 |
doc = Document()
|