Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,15 @@
|
|
1 |
import streamlit as st
|
2 |
from fpdf import FPDF
|
|
|
3 |
|
4 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def process_text(input_text):
|
6 |
lines = input_text.split('\n')
|
7 |
structured_text = []
|
@@ -17,23 +25,24 @@ def process_text(input_text):
|
|
17 |
structured_text.append({'type': 'paragraph', 'text': line})
|
18 |
return structured_text
|
19 |
|
20 |
-
# Function to generate PDF
|
21 |
def generate_pdf(structured_text):
|
22 |
pdf = FPDF()
|
23 |
pdf.set_auto_page_break(auto=True, margin=15)
|
24 |
pdf.add_page()
|
25 |
pdf.set_font("Arial", size=12)
|
26 |
-
|
27 |
for item in structured_text:
|
|
|
28 |
if item['type'] == 'heading':
|
29 |
pdf.set_font("Arial", size=16, style='B')
|
30 |
-
pdf.cell(0, 10, txt=
|
31 |
elif item['type'] == 'subheading':
|
32 |
pdf.set_font("Arial", size=14, style='B')
|
33 |
-
pdf.cell(0, 10, txt=
|
34 |
else: # Paragraph or body text
|
35 |
pdf.set_font("Arial", size=12)
|
36 |
-
pdf.multi_cell(0, 10, txt=
|
37 |
|
38 |
# Save PDF in-memory
|
39 |
return pdf.output(dest='S').encode('latin1')
|
@@ -50,6 +59,11 @@ input_text = st.text_area("Enter your text below:", height=300)
|
|
50 |
|
51 |
if st.button("Generate PDF"):
|
52 |
if input_text.strip():
|
|
|
|
|
|
|
|
|
|
|
53 |
structured_text = process_text(input_text)
|
54 |
pdf_data = generate_pdf(structured_text)
|
55 |
|
@@ -62,3 +76,4 @@ if st.button("Generate PDF"):
|
|
62 |
)
|
63 |
else:
|
64 |
st.error("Please enter some text before generating the PDF.")
|
|
|
|
1 |
import streamlit as st
|
2 |
from fpdf import FPDF
|
3 |
+
import unicodedata
|
4 |
|
5 |
+
# Function to sanitize text by removing unsupported characters
|
6 |
+
def sanitize_text(text):
|
7 |
+
return ''.join(
|
8 |
+
c for c in unicodedata.normalize('NFKD', text)
|
9 |
+
if ord(c) < 128 # Retain only ASCII characters
|
10 |
+
)
|
11 |
+
|
12 |
+
# Function to identify headings, subheadings, and paragraphs
|
13 |
def process_text(input_text):
|
14 |
lines = input_text.split('\n')
|
15 |
structured_text = []
|
|
|
25 |
structured_text.append({'type': 'paragraph', 'text': line})
|
26 |
return structured_text
|
27 |
|
28 |
+
# Function to generate a PDF
|
29 |
def generate_pdf(structured_text):
|
30 |
pdf = FPDF()
|
31 |
pdf.set_auto_page_break(auto=True, margin=15)
|
32 |
pdf.add_page()
|
33 |
pdf.set_font("Arial", size=12)
|
34 |
+
|
35 |
for item in structured_text:
|
36 |
+
sanitized_text = sanitize_text(item['text'])
|
37 |
if item['type'] == 'heading':
|
38 |
pdf.set_font("Arial", size=16, style='B')
|
39 |
+
pdf.cell(0, 10, txt=sanitized_text, ln=True)
|
40 |
elif item['type'] == 'subheading':
|
41 |
pdf.set_font("Arial", size=14, style='B')
|
42 |
+
pdf.cell(0, 10, txt=sanitized_text, ln=True)
|
43 |
else: # Paragraph or body text
|
44 |
pdf.set_font("Arial", size=12)
|
45 |
+
pdf.multi_cell(0, 10, txt=sanitized_text)
|
46 |
|
47 |
# Save PDF in-memory
|
48 |
return pdf.output(dest='S').encode('latin1')
|
|
|
59 |
|
60 |
if st.button("Generate PDF"):
|
61 |
if input_text.strip():
|
62 |
+
# Warn the user if unsupported characters are removed
|
63 |
+
if any(ord(char) > 255 for char in input_text):
|
64 |
+
st.warning("Some special characters or emojis have been removed to generate the PDF.")
|
65 |
+
|
66 |
+
# Process the input text and generate the PDF
|
67 |
structured_text = process_text(input_text)
|
68 |
pdf_data = generate_pdf(structured_text)
|
69 |
|
|
|
76 |
)
|
77 |
else:
|
78 |
st.error("Please enter some text before generating the PDF.")
|
79 |
+
|