-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_pdf_document_from_html.py
More file actions
63 lines (55 loc) · 2.44 KB
/
get_pdf_document_from_html.py
File metadata and controls
63 lines (55 loc) · 2.44 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
from bs4 import BeautifulSoup
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, ListFlowable, ListItem
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.enums import TA_JUSTIFY
from io import BytesIO
def generate_pdf_document(content):
byte_stream = BytesIO()
doc = SimpleDocTemplate(byte_stream, pagesize=letter)
styles = getSampleStyleSheet()
story = []
soup = BeautifulSoup(content, 'html.parser')
def add_element_to_story(element):
text = element.get_text(strip=True)
if not text and element.name not in ['br', 'hr']:
return
if element.name == 'h1':
story.append(Paragraph(text, styles['Heading1']))
story.append(Spacer(1, 12))
elif element.name == 'h2':
story.append(Paragraph(text, styles['Heading2']))
story.append(Spacer(1, 10))
elif element.name == 'h3':
story.append(Paragraph(text, styles['Heading3']))
story.append(Spacer(1, 8))
elif element.name == 'p':
story.append(Paragraph(text, styles['Normal']))
story.append(Spacer(1, 12))
elif element.name == 'ul':
list_items = []
for li in element.find_all('li'):
list_items.append(ListItem(Paragraph(li.get_text(strip=True), styles['Normal'])))
story.append(ListFlowable(list_items, bulletType='bullet', start='circle'))
story.append(Spacer(1, 12))
elif element.name == 'ol':
list_items = []
for li in element.find_all('li'):
list_items.append(ListItem(Paragraph(li.get_text(strip=True), styles['Normal'])))
story.append(ListFlowable(list_items, bulletType='1'))
story.append(Spacer(1, 12))
elif element.name == 'strong' or element.name == 'b':
story.append(Paragraph(f"<b>{text}</b>", styles['Normal']))
elif element.name == 'em' or element.name == 'i':
story.append(Paragraph(f"<i>{text}</i>", styles['Normal']))
elif element.name:
story.append(Paragraph(text, styles['Normal']))
story.append(Spacer(1, 12))
for element in soup.contents:
if element.name:
add_element_to_story(element)
try:
doc.build(story)
return byte_stream.getvalue()
except Exception as e:
return b""