Files
fn_registry/python/functions/infra/pdf_save.py
T
egutierrez 0819c35bbb feat: issue/0020 — generacion de PDFs en Python y Go
Añade 3 tipos Python (PDFDoc, PDFPage, PDFStyle) y 10 funciones Python
para construir PDFs con fpdf2 (builder fluent), fusionar PDFs con pypdf
y convertir HTML/Markdown a PDF via weasyprint (stub si no disponible).
Añade pdf_simple_report en Go como stub hasta que go-pdf/fpdf se integre.

- python/types/infra/: pdf_doc, pdf_page, pdf_style
- python/functions/infra/: pdf_create, pdf_add_page, pdf_add_text,
  pdf_add_table, pdf_add_image, pdf_add_header_footer, pdf_from_html,
  pdf_from_markdown, pdf_merge, pdf_save
- functions/infra/pdf_simple_report.go: stub Go con ReportSection/ReportTable
- 17 tests Python pasando (pytest)
- fpdf2 y pypdf añadidos via uv al venv Python

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 02:02:51 +02:00

32 lines
953 B
Python

"""pdf_save — guarda el documento PDF a un archivo o retorna bytes."""
import sys
import os
_types_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "python", "types", "infra")
sys.path.insert(0, _types_dir)
from pdf_doc import PDFDoc
def pdf_save(
doc: PDFDoc,
output_path: str | None = None,
) -> str | bytes:
"""Guarda el documento PDF a un archivo o retorna los bytes del PDF.
Si se especifica output_path, escribe el PDF en disco y retorna el path.
Si output_path es None, retorna los bytes del PDF sin escribir a disco.
Args:
doc: PDFDoc con al menos una pagina de contenido.
output_path: ruta del archivo PDF a crear. None retorna bytes.
Returns:
output_path si se especifico, o bytes del PDF si output_path es None.
"""
if output_path is not None:
doc.fpdf.output(output_path)
return output_path
else:
return bytes(doc.fpdf.output())