Spaces:
Sleeping
Sleeping
Update genesis/utils/pdf_export.py
Browse files- genesis/utils/pdf_export.py +27 -41
genesis/utils/pdf_export.py
CHANGED
@@ -1,55 +1,41 @@
|
|
|
|
1 |
import os
|
2 |
from reportlab.lib.pagesizes import letter
|
3 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
4 |
from reportlab.lib.styles import getSampleStyleSheet
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
filepath = os.path.join(os.getcwd(), filename)
|
9 |
doc = SimpleDocTemplate(filepath, pagesize=letter)
|
10 |
styles = getSampleStyleSheet()
|
11 |
story = []
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
# Query
|
18 |
-
story.append(Paragraph(f"<b>Query:</b> {query}", styles["Normal"]))
|
19 |
-
story.append(Spacer(1, 12))
|
20 |
-
|
21 |
-
# Summary
|
22 |
-
story.append(Paragraph("<b>Summary:</b>", styles["Heading2"]))
|
23 |
-
story.append(Paragraph(summary, styles["Normal"]))
|
24 |
-
story.append(Spacer(1, 12))
|
25 |
-
|
26 |
-
# Citations
|
27 |
-
if citations:
|
28 |
-
story.append(Paragraph("<b>Citations:</b>", styles["Heading2"]))
|
29 |
-
for c in citations:
|
30 |
-
story.append(Paragraph(f"{c.get('type', '')}: {c.get('url', '')}", styles["Normal"]))
|
31 |
-
story.append(Spacer(1, 12))
|
32 |
-
|
33 |
-
# Structures
|
34 |
-
if structures:
|
35 |
-
story.append(Paragraph("<b>Structures:</b>", styles["Heading2"]))
|
36 |
-
for s in structures:
|
37 |
-
story.append(Paragraph(str(s), styles["Normal"]))
|
38 |
story.append(Spacer(1, 12))
|
39 |
|
40 |
-
#
|
41 |
-
if
|
42 |
-
story.append(Paragraph("<b>
|
43 |
-
for
|
44 |
-
story.append(Paragraph(
|
45 |
-
|
46 |
-
|
47 |
-
# Regulation
|
48 |
-
if regulation_data:
|
49 |
-
story.append(Paragraph("<b>Regulatory Info:</b>", styles["Heading2"]))
|
50 |
-
for r in regulation_data:
|
51 |
-
story.append(Paragraph(str(r), styles["Normal"]))
|
52 |
-
story.append(Spacer(1, 12))
|
53 |
|
54 |
doc.build(story)
|
55 |
return filepath
|
|
|
|
|
|
|
|
1 |
+
# genesis/utils/pdf_export.py
|
2 |
import os
|
3 |
from reportlab.lib.pagesizes import letter
|
4 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
5 |
from reportlab.lib.styles import getSampleStyleSheet
|
6 |
+
from reportlab.lib.utils import escape
|
7 |
+
|
8 |
+
def _format_content(content):
|
9 |
+
"""Convert string, list, or dict into clean string for PDF."""
|
10 |
+
if isinstance(content, str):
|
11 |
+
return content
|
12 |
+
elif isinstance(content, dict):
|
13 |
+
return "\n".join([f"<b>{escape(str(k))}</b>: {escape(str(v))}" for k, v in content.items()])
|
14 |
+
elif isinstance(content, list):
|
15 |
+
return "\n".join([escape(str(item)) for item in content])
|
16 |
+
return escape(str(content))
|
17 |
+
|
18 |
+
def export_to_pdf(content, extra=None, filename="output.pdf"):
|
19 |
+
"""Export given content to a PDF file."""
|
20 |
filepath = os.path.join(os.getcwd(), filename)
|
21 |
doc = SimpleDocTemplate(filepath, pagesize=letter)
|
22 |
styles = getSampleStyleSheet()
|
23 |
story = []
|
24 |
|
25 |
+
# Main content
|
26 |
+
for line in _format_content(content).split("\n"):
|
27 |
+
story.append(Paragraph(line, styles["Normal"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
story.append(Spacer(1, 12))
|
29 |
|
30 |
+
# Extra info (like citations)
|
31 |
+
if extra:
|
32 |
+
story.append(Paragraph("<b>Extra Information:</b>", styles["Heading2"]))
|
33 |
+
for line in _format_content(extra).split("\n"):
|
34 |
+
story.append(Paragraph(line, styles["Normal"]))
|
35 |
+
story.append(Spacer(1, 12))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
doc.build(story)
|
38 |
return filepath
|
39 |
+
|
40 |
+
# Backwards compatibility
|
41 |
+
export_report_to_pdf = export_to_pdf
|