File size: 991 Bytes
bc5615f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dd7bcd
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
from reportlab.lib.pagesizes import LETTER
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
import io


def render_report_pdf(text: str, title: str = "Equity Research Report") -> bytes:
	buf = io.BytesIO()
	c = canvas.Canvas(buf, pagesize=LETTER)
	width, height = LETTER

	# Title
	c.setFont("Helvetica-Bold", 16)
	c.drawString(1 * inch, height - 1 * inch, title)

	# Body
	c.setFont("Helvetica", 10)
	x = 1 * inch
	y = height - 1.25 * inch
	max_width = width - 2 * inch

	def draw_wrapped(text_line: str):
		from reportlab.lib.utils import simpleSplit
		wrapped = simpleSplit(text_line, "Helvetica", 10, max_width)
		nonlocal y
		for part in wrapped:
			if y < 1 * inch:
				c.showPage()
				c.setFont("Helvetica", 10)
				y = height - 1 * inch
			c.drawString(x, y, part)
			y -= 12

	for line in text.splitlines():
		draw_wrapped(line if line.strip() else " ")

	c.showPage()
	c.save()
	buf.seek(0)
	return buf.read()