File size: 1,086 Bytes
37b725a
52bfcb4
 
 
 
8693633
37b725a
 
 
52bfcb4
 
37b725a
 
52bfcb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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()