-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdf.py
More file actions
228 lines (194 loc) · 7.26 KB
/
Copy pathpdf.py
File metadata and controls
228 lines (194 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
API route for PDF generation — mirrors src/app/api/generate-pdf/route.ts
POST /api/generate-pdf
"""
import io
import logging
import re
from flask import Blueprint, Response, jsonify, request
bp = Blueprint("pdf", __name__)
_logger = logging.getLogger(__name__)
def _safe_text(text: str) -> str:
"""Replace characters that fpdf2's built-in fonts cannot render."""
if not text:
return ""
# Replace common problematic Unicode characters with ASCII equivalents
replacements = {
"\u2018": "'", "\u2019": "'", # smart quotes
"\u201c": '"', "\u201d": '"',
"\u2013": "-", "\u2014": "--",
"\u2026": "...",
"\u2022": "*", # bullet
"\u00a0": " ", # non-breaking space
"\u200b": "", # zero-width space
"\u2003": " ", # em space
"\ufeff": "", # BOM
}
for old, new in replacements.items():
text = text.replace(old, new)
# Remove any remaining non-latin1 characters
result = []
for ch in text:
try:
ch.encode("latin-1")
result.append(ch)
except UnicodeEncodeError:
result.append("?")
return "".join(result)
@bp.route("/api/generate-pdf", methods=["POST"])
def generate_pdf():
try:
body = request.get_json(silent=True) or {}
markdown_text = body.get("markdown", "")
title = body.get("title", "Chat")
from fpdf import FPDF
class PDFDoc(FPDF):
def header(self):
pass
def footer(self):
self.set_y(-15)
self.set_font("Helvetica", "I", 8)
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="C")
pdf = PDFDoc()
pdf.alias_nb_pages()
pdf.set_auto_page_break(auto=True, margin=25)
pdf.add_page()
pdf.set_margins(20, 20, 20)
# Title
pdf.set_font("Helvetica", "B", 20)
pdf.cell(0, 12, _safe_text(title), new_x="LMARGIN", new_y="NEXT")
pdf.ln(8)
# Parse markdown line by line
lines = markdown_text.split("\n")
in_code_block = False
code_lines = []
for line in lines:
try:
# Code block toggle
if line.strip().startswith("```"):
if in_code_block:
# End code block -> render collected lines
_render_code_block(pdf, "\n".join(code_lines))
code_lines = []
in_code_block = False
else:
in_code_block = True
continue
if in_code_block:
code_lines.append(line)
continue
# Headings
if line.startswith("### "):
pdf.ln(4)
pdf.set_font("Helvetica", "B", 13)
pdf.set_text_color(96, 165, 250)
pdf.cell(0, 8, _safe_text(line[4:]), new_x="LMARGIN", new_y="NEXT")
pdf.set_text_color(0, 0, 0)
continue
if line.startswith("## "):
pdf.ln(4)
pdf.set_font("Helvetica", "B", 15)
pdf.set_text_color(59, 130, 246)
pdf.cell(0, 9, _safe_text(line[3:]), new_x="LMARGIN", new_y="NEXT")
pdf.set_text_color(0, 0, 0)
continue
if line.startswith("# "):
pdf.ln(4)
pdf.set_font("Helvetica", "B", 18)
pdf.set_text_color(37, 99, 235)
pdf.cell(0, 10, _safe_text(line[2:]), new_x="LMARGIN", new_y="NEXT")
pdf.set_text_color(0, 0, 0)
continue
# Horizontal rule
if line.strip() == "---":
pdf.ln(3)
y = pdf.get_y()
pdf.set_draw_color(180, 180, 180)
pdf.line(20, y, pdf.w - 20, y)
pdf.ln(3)
continue
# Italic line (metadata)
if line.startswith("_") and line.endswith("_"):
pdf.set_font("Helvetica", "I", 9)
pdf.set_text_color(120, 120, 120)
pdf.multi_cell(0, 5, _safe_text(line.strip("_")))
pdf.set_text_color(0, 0, 0)
continue
# List items
if line.startswith("- "):
pdf.set_font("Helvetica", "", 10)
pdf.cell(5, 5, "*") # bullet (using safe ASCII)
pdf.multi_cell(0, 5, _safe_text(line[2:]))
continue
# Normal paragraph
if line.strip():
pdf.set_font("Helvetica", "", 10)
# Strip markdown bold/italic for PDF
clean = re.sub(r"\*\*(.+?)\*\*", r"\1", line)
clean = re.sub(r"\*(.+?)\*", r"\1", clean)
clean = re.sub(r"`(.+?)`", r"\1", clean)
pdf.multi_cell(0, 5, _safe_text(clean))
else:
pdf.ln(3)
except Exception:
# Skip lines that cause rendering errors
continue
buf = io.BytesIO()
pdf.output(buf)
buf.seek(0)
safe_title = re.sub(r'[<>:"/\\|?*]', '_', title)
return Response(
buf.getvalue(),
mimetype="application/pdf",
headers={
"Content-Disposition": f'attachment; filename="{safe_title}.pdf"',
},
)
except Exception as e:
_logger.error(
"Failed to generate PDF: %s (%s)",
e,
type(e).__name__,
exc_info=True,
)
return jsonify({"error": "Failed to generate PDF"}), 500
def _render_code_block(pdf, code_text: str):
"""Render a code block with a dark background."""
pdf.ln(3)
pdf.set_font("Courier", "", 8)
pdf.set_fill_color(30, 30, 30)
pdf.set_text_color(212, 212, 212)
# Calculate needed height
lines = code_text.split("\n")
line_height = 4
block_height = len(lines) * line_height + 6 # padding
# Cap the block height to avoid overflow
max_block_height = pdf.h - 50
if block_height > max_block_height:
block_height = max_block_height
# Check if we need a page break
if pdf.get_y() + min(block_height, 60) > pdf.h - 25:
pdf.add_page()
x = pdf.get_x()
y = pdf.get_y()
w = pdf.w - 40 # margins
# Background rectangle
pdf.rect(x, y, w, block_height, "F")
# Text
pdf.set_xy(x + 3, y + 3)
for i, line in enumerate(lines):
if pdf.get_y() > pdf.h - 25:
pdf.add_page()
pdf.set_font("Courier", "", 8)
pdf.set_fill_color(30, 30, 30)
pdf.set_text_color(212, 212, 212)
try:
pdf.cell(0, line_height, _safe_text(line), new_x="LMARGIN", new_y="NEXT")
except Exception:
pdf.cell(0, line_height, "[encoding error]", new_x="LMARGIN", new_y="NEXT")
if i < len(lines) - 1:
pdf.set_x(x + 3)
pdf.set_y(y + block_height + 2)
pdf.set_text_color(0, 0, 0)
pdf.set_font("Helvetica", "", 10)
pdf.ln(3)