QureadAI / quread /export_pdf.py
hchevva's picture
Update quread/export_pdf.py
52bfcb4 verified
# quread/export_pdf.py
from reportlab.lib.pagesizes import LETTER
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
import textwrap
def md_to_pdf(markdown_text: str, output_path: str):
"""
Convert Markdown-like text to a simple PDF.
Pure Python, HF-Spaces safe (no system deps).
"""
c = canvas.Canvas(output_path, pagesize=LETTER)
width, height = LETTER
x_margin = 1 * inch
y_margin = 1 * inch
max_width = width - 2 * x_margin
text_obj = c.beginText()
text_obj.setTextOrigin(x_margin, height - y_margin)
text_obj.setFont("Times-Roman", 11)
for line in markdown_text.splitlines():
wrapped = textwrap.wrap(line, 95) or [""]
for wline in wrapped:
if text_obj.getY() < y_margin:
c.drawText(text_obj)
c.showPage()
text_obj = c.beginText()
text_obj.setTextOrigin(x_margin, height - y_margin)
text_obj.setFont("Times-Roman", 11)
text_obj.textLine(wline)
c.drawText(text_obj)
c.save()