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>
This commit is contained in:
2026-04-13 02:02:51 +02:00
parent 092f14eff0
commit df424f2de0
35 changed files with 1818 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
---
name: pdf_from_html
kind: function
lang: py
domain: infra
version: "1.0.0"
purity: impure
signature: "def pdf_from_html(html_string, output_path, page_size, css) -> str"
description: "Convierte un HTML string a PDF usando weasyprint con soporte completo de CSS layout. Retorna error claro si weasyprint no esta instalado (requiere dependencias de sistema: pango, cairo)."
tags: [pdf, html, weasyprint, conversion, infra]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: [weasyprint]
params:
- name: html_string
desc: "contenido HTML completo como string, incluyendo head y body"
- name: output_path
desc: "ruta del archivo PDF a generar"
- name: page_size
desc: "tamaño de pagina CSS: A4, letter, A3. Por defecto A4"
- name: css
desc: "CSS adicional como string para aplicar sobre el HTML, puede incluir fuentes y layouts"
output: "output_path con el PDF guardado en disco"
tested: false
tests: []
test_file_path: ""
file_path: "python/functions/infra/pdf_from_html.py"
---
## Ejemplo
```python
html = """
<!DOCTYPE html>
<html>
<body>
<h1>Reporte</h1>
<table>
<tr><th>Dominio</th><th>Funciones</th></tr>
<tr><td>core</td><td>45</td></tr>
</table>
</body>
</html>
"""
css = "body { font-family: sans-serif; } h1 { color: #333; }"
output = pdf_from_html(html, "reporte.pdf", css=css)
```
## Instalacion de dependencias
```bash
# Python
pip install weasyprint
# Linux (Ubuntu/Debian)
sudo apt install libpango-1.0-0 libcairo2 libgdk-pixbuf-2.0-0
# macOS
brew install pango cairo gdk-pixbuf
```
## Notas
Stub activo: si `weasyprint` no esta instalado, lanza `RuntimeError` con instrucciones de instalacion claras. Para la mayoria de reportes simples, preferir el builder fpdf2 (`pdf_create` + `pdf_add_text` + `pdf_add_table`) que no requiere dependencias de sistema. weasyprint es util para HTML complejo con CSS avanzado (columnas, grid, fuentes web, estilos corporativos).