-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_to_pdf.py
49 lines (41 loc) · 1.61 KB
/
str_to_pdf.py
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
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_LEFT
def save_string_to_pdf(text, filename):
# Create a SimpleDocTemplate with letter size
doc = SimpleDocTemplate(filename, pagesize=letter)
# Define custom styles for headings and body text
heading_style = ParagraphStyle(
name="Heading",
fontName='Helvetica-Bold',
fontSize=14,
leading=18, # Line spacing for heading
alignment=TA_LEFT,
spaceAfter=14, # Space after heading
textColor=colors.black
)
body_style = ParagraphStyle(
name="Body",
fontName='Times-Roman',
fontSize=12,
leading=14, # Line spacing for body text
alignment=TA_LEFT,
textColor=colors.black
)
# Split the input text by newlines to create individual paragraphs
lines = text.split('\n')
# List to hold paragraphs
story = []
# Add a heading if necessary
# story.append(Paragraph("Cover Letter", heading_style))
# story.append(Spacer(1, 24)) # Add space after the heading
# Create a Paragraph object for each line of the body text
for line in lines:
if line.strip(): # Only create a paragraph if the line is not empty
paragraph = Paragraph(line, body_style)
story.append(paragraph)
story.append(Spacer(1, 12)) # Add space between paragraphs (12 points)
# Build the PDF document
doc.build(story)