Export the report to PDF.
(
_context: Dict[str, Any] | None = None,
)
| 532 | return f"Deleted chapter '{title}'" |
| 533 | |
| 534 | def report_export_pdf( |
| 535 | _context: Dict[str, Any] | None = None, |
| 536 | ) -> List[MessageBlock]: |
| 537 | """ |
| 538 | Export the report to PDF. |
| 539 | """ |
| 540 | ctx = FileToolContext(_context) |
| 541 | _, report_file = _get_files(ctx) |
| 542 | _, report_lock = _get_locks(ctx) |
| 543 | |
| 544 | with FileLock(report_lock): |
| 545 | if not report_file.exists(): |
| 546 | raise FileNotFoundError("Report file does not exist.") |
| 547 | text = report_file.read_text(encoding="utf-8") |
| 548 | |
| 549 | text = re.sub(r"([^\n])\n(#{1,6}\s)", r"\1\n\n\2", text) |
| 550 | text = re.sub(r"(?m)^(?!\s*(?:[*+-]|\d+\.)\s)(.+)\n(\s*(?:[*+-]|\d+\.)\s)", r"\1\n\n\2", text) |
| 551 | |
| 552 | try: |
| 553 | import markdown |
| 554 | from xhtml2pdf import pisa |
| 555 | except ImportError: |
| 556 | raise ImportError( |
| 557 | "Error: strict dependencies 'markdown' and 'xhtml2pdf' are missing." |
| 558 | ) |
| 559 | |
| 560 | pdf_file = report_file.with_suffix(".pdf") |
| 561 | |
| 562 | # Convert to HTML |
| 563 | extensions = ["extra", "codehilite", "nl2br", "tables"] |
| 564 | html_content = markdown.markdown(text, extensions=extensions) |
| 565 | |
| 566 | styled_html = f""" |
| 567 | <html> |
| 568 | <head> |
| 569 | <style> |
| 570 | @page {{ |
| 571 | size: A4; |
| 572 | margin: 2cm; |
| 573 | }} |
| 574 | body {{ |
| 575 | font-family: sans-serif; |
| 576 | line-height: 1.6; |
| 577 | font-size: 10pt; |
| 578 | word-wrap: break-word; |
| 579 | word-break: break-all; |
| 580 | }} |
| 581 | h1, h2, h3 {{ |
| 582 | color: #2c3e50; |
| 583 | margin-top: 25px; /* Add spacing above the title */ |
| 584 | margin-bottom: 15px; |
| 585 | border-bottom: 1px solid #eee; /* Add an underline to the main title for clarity */ |
| 586 | padding-bottom: 5px; |
| 587 | }} |
| 588 | |
| 589 | /* --- Table style fixes --- */ |
| 590 | table {{ |
| 591 | width: 100%; |
nothing calls this directly
no test coverage detected