-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeneratePDFwithText.py
More file actions
38 lines (28 loc) · 1.07 KB
/
GeneratePDFwithText.py
File metadata and controls
38 lines (28 loc) · 1.07 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
from reportlab.pdfgen import canvas
def generate_pdf(text, file_name='output.pdf'):
# Create a PDF canvas
pdf = canvas.Canvas(file_name)
# Set page size
page_width, page_height = 400, 600 # You can adjust these values as needed
pdf.setPageSize((page_width, page_height))
# Coordinates to start writing text
x, y = 50, page_height - 100
# Maximum characters per line
max_characters_per_line = 50
# Split the text into lines
lines = [text[i:i + max_characters_per_line] for i in range(0, len(text), max_characters_per_line)]
# Write each line to the PDF canvas
for line in lines:
pdf.drawString(x, y, line)
y -= 20 # Adjust the line spacing as needed
# Save the PDF
pdf.save()
print(f"PDF file created: {file_name}")
if __name__ == "__main__":
# You can change the text as per your preference
text_for_pdf = """
This is an example text for the PDF.
You can provide your own text when running the script.
"""
# Generate the PDF with the provided text
generate_pdf(text_for_pdf)