Spaces:
Sleeping
Sleeping
- vault_logic.py +26 -8
vault_logic.py
CHANGED
|
@@ -158,7 +158,13 @@ def create_pdf(title, content):
|
|
| 158 |
pdf.set_font("Arial", "B", 16)
|
| 159 |
|
| 160 |
# Write Title
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
pdf.ln(5)
|
| 163 |
|
| 164 |
# Write Content
|
|
@@ -166,18 +172,30 @@ def create_pdf(title, content):
|
|
| 166 |
pdf.set_font('DejaVu', '', 12)
|
| 167 |
else:
|
| 168 |
pdf.set_font("helvetica", "", 12)
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# Output as bytes
|
| 173 |
-
# Get PDF as bytes
|
| 174 |
pdf_bytes = pdf.output(dest='S')
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
except Exception as e:
|
| 177 |
-
# Return error as bytes
|
| 178 |
-
error_msg = f"
|
| 179 |
return error_msg.encode('utf-8')
|
| 180 |
-
|
| 181 |
def create_docx(title, content):
|
| 182 |
doc = Document()
|
| 183 |
doc.add_heading(title, 0)
|
|
|
|
| 158 |
pdf.set_font("Arial", "B", 16)
|
| 159 |
|
| 160 |
# Write Title
|
| 161 |
+
# Handle potential Unicode issues in title
|
| 162 |
+
try:
|
| 163 |
+
pdf.multi_cell(0, 10, title)
|
| 164 |
+
except:
|
| 165 |
+
# If title has Unicode issues, clean it
|
| 166 |
+
clean_title = title.encode('ascii', 'ignore').decode('ascii')
|
| 167 |
+
pdf.multi_cell(0, 10, clean_title)
|
| 168 |
pdf.ln(5)
|
| 169 |
|
| 170 |
# Write Content
|
|
|
|
| 172 |
pdf.set_font('DejaVu', '', 12)
|
| 173 |
else:
|
| 174 |
pdf.set_font("helvetica", "", 12)
|
| 175 |
+
|
| 176 |
+
# Handle potential Unicode issues in content
|
| 177 |
+
try:
|
| 178 |
+
pdf.multi_cell(0, 10, content)
|
| 179 |
+
except:
|
| 180 |
+
# If content has Unicode issues, clean it
|
| 181 |
+
clean_content = content.encode('ascii', 'ignore').decode('ascii')
|
| 182 |
+
pdf.multi_cell(0, 10, clean_content)
|
| 183 |
|
| 184 |
# Output as bytes
|
|
|
|
| 185 |
pdf_bytes = pdf.output(dest='S')
|
| 186 |
+
|
| 187 |
+
# Verify it's a valid PDF (starts with %PDF)
|
| 188 |
+
if pdf_bytes and pdf_bytes.startswith(b'%PDF'):
|
| 189 |
+
return pdf_bytes
|
| 190 |
+
else:
|
| 191 |
+
# If not valid PDF, return error
|
| 192 |
+
return b"PDF_ERROR: Generated PDF is invalid"
|
| 193 |
+
|
| 194 |
except Exception as e:
|
| 195 |
+
# Return error as bytes with consistent prefix
|
| 196 |
+
error_msg = f"PDF_ERROR: {str(e)}"
|
| 197 |
return error_msg.encode('utf-8')
|
| 198 |
+
|
| 199 |
def create_docx(title, content):
|
| 200 |
doc = Document()
|
| 201 |
doc.add_heading(title, 0)
|