Files
fn_registry/python/functions/infra/pdf_merge.py
T
egutierrez df424f2de0 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

51 lines
1.3 KiB
Python

"""pdf_merge — fusiona multiples archivos PDF en uno solo usando pypdf."""
import os
def pdf_merge(
pdf_paths: list[str],
output_path: str,
) -> str:
"""Fusiona una lista de archivos PDF en un unico PDF.
Une los PDFs en el orden de la lista. Requiere pypdf instalado.
Args:
pdf_paths: lista de rutas a los archivos PDF a fusionar (en orden).
output_path: ruta del archivo PDF combinado a generar.
Returns:
output_path con el PDF fusionado guardado.
Raises:
FileNotFoundError: si alguno de los archivos no existe.
RuntimeError: si pypdf no esta instalado.
ValueError: si pdf_paths esta vacio.
"""
if not pdf_paths:
raise ValueError("pdf_paths no puede estar vacio")
for path in pdf_paths:
if not os.path.exists(path):
raise FileNotFoundError(f"PDF no encontrado: {path}")
try:
from pypdf import PdfWriter, PdfReader
except ImportError as exc:
raise RuntimeError(
"pypdf no esta instalado. Instalar con: pip install pypdf"
) from exc
writer = PdfWriter()
for path in pdf_paths:
reader = PdfReader(path)
for page in reader.pages:
writer.add_page(page)
with open(output_path, "wb") as f:
writer.write(f)
return output_path