Files
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

51 lines
1.5 KiB
Python

"""Tests para pdf_merge."""
import sys
import os
import tempfile
import pytest
sys.path.insert(0, os.path.dirname(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "python", "types", "infra"))
from pdf_create import pdf_create
from pdf_add_page import pdf_add_page
from pdf_add_text import pdf_add_text
from pdf_save import pdf_save
from pdf_merge import pdf_merge
def _create_pdf(path: str, text: str) -> str:
doc = pdf_create(title=text)
doc = pdf_add_page(doc)
doc = pdf_add_text(doc, text)
return pdf_save(doc, path)
def test_fusionar_dos_pdfs():
with tempfile.TemporaryDirectory() as tmpdir:
p1 = _create_pdf(os.path.join(tmpdir, "a.pdf"), "Documento A")
p2 = _create_pdf(os.path.join(tmpdir, "b.pdf"), "Documento B")
out = os.path.join(tmpdir, "merged.pdf")
result = pdf_merge([p1, p2], out)
assert result == out
assert os.path.exists(out)
# Verificar que el PDF combinado es valido y mas grande que cada parte
with open(out, "rb") as f:
content = f.read()
assert content[:4] == b"%PDF"
def test_error_si_archivo_no_existe():
with tempfile.TemporaryDirectory() as tmpdir:
out = os.path.join(tmpdir, "merged.pdf")
with pytest.raises(FileNotFoundError):
pdf_merge(["/no/existe.pdf"], out)
def test_error_si_lista_vacia():
with tempfile.TemporaryDirectory() as tmpdir:
out = os.path.join(tmpdir, "merged.pdf")
with pytest.raises(ValueError):
pdf_merge([], out)