BaoKhuong's picture
Upload 5 files
5dd7bcd verified
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()