Update quread/export_pdf.py
Browse files- quread/export_pdf.py +30 -8
quread/export_pdf.py
CHANGED
|
@@ -1,15 +1,37 @@
|
|
| 1 |
# quread/export_pdf.py
|
| 2 |
-
from
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def md_to_pdf(markdown_text: str, output_path: str):
|
| 7 |
"""
|
| 8 |
-
Convert Markdown text to a PDF
|
|
|
|
| 9 |
"""
|
| 10 |
-
html = markdown(
|
| 11 |
-
markdown_text,
|
| 12 |
-
extensions=["fenced_code", "tables"]
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# quread/export_pdf.py
|
| 2 |
+
from reportlab.lib.pagesizes import LETTER
|
| 3 |
+
from reportlab.pdfgen import canvas
|
| 4 |
+
from reportlab.lib.units import inch
|
| 5 |
+
import textwrap
|
| 6 |
|
| 7 |
|
| 8 |
def md_to_pdf(markdown_text: str, output_path: str):
|
| 9 |
"""
|
| 10 |
+
Convert Markdown-like text to a simple PDF.
|
| 11 |
+
Pure Python, HF-Spaces safe (no system deps).
|
| 12 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
c = canvas.Canvas(output_path, pagesize=LETTER)
|
| 15 |
+
width, height = LETTER
|
| 16 |
+
|
| 17 |
+
x_margin = 1 * inch
|
| 18 |
+
y_margin = 1 * inch
|
| 19 |
+
max_width = width - 2 * x_margin
|
| 20 |
+
|
| 21 |
+
text_obj = c.beginText()
|
| 22 |
+
text_obj.setTextOrigin(x_margin, height - y_margin)
|
| 23 |
+
text_obj.setFont("Times-Roman", 11)
|
| 24 |
+
|
| 25 |
+
for line in markdown_text.splitlines():
|
| 26 |
+
wrapped = textwrap.wrap(line, 95) or [""]
|
| 27 |
+
for wline in wrapped:
|
| 28 |
+
if text_obj.getY() < y_margin:
|
| 29 |
+
c.drawText(text_obj)
|
| 30 |
+
c.showPage()
|
| 31 |
+
text_obj = c.beginText()
|
| 32 |
+
text_obj.setTextOrigin(x_margin, height - y_margin)
|
| 33 |
+
text_obj.setFont("Times-Roman", 11)
|
| 34 |
+
text_obj.textLine(wline)
|
| 35 |
+
|
| 36 |
+
c.drawText(text_obj)
|
| 37 |
+
c.save()
|