Files
egutierrez 47fac22230 chore: auto-commit (799 archivos)
- .claude/CLAUDE.md
- .claude/commands/subagentes.md
- .claude/rules/INDEX.md
- .mcp.json
- bash/functions/cybersecurity/analyze_dns.md
- bash/functions/cybersecurity/audit_http_headers.md
- bash/functions/cybersecurity/audit_ssh_config.md
- bash/functions/cybersecurity/check_firewall.md
- bash/functions/cybersecurity/detect_suspicious_users.md
- bash/functions/cybersecurity/encrypt_file.md
- ...

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 00:28:20 +02:00

47 lines
1.7 KiB
Markdown

---
name: pdf_save
kind: function
lang: py
domain: infra
version: "1.0.0"
purity: impure
signature: "def pdf_save(doc, output_path) -> str | bytes"
description: "Serializa el documento PDF a disco o retorna los bytes en memoria. Si output_path es None retorna bytes para transmision directa (HTTP response, almacenamiento en BD). Si se especifica, escribe el archivo y retorna el path."
tags: [pdf, save, output, builder, infra, pendiente-usar]
uses_functions: []
uses_types: [pdf_doc_py_infra]
returns: []
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: doc
desc: "PDFDoc con al menos una pagina y contenido añadido"
- name: output_path
desc: "ruta del archivo PDF a crear en disco; None retorna bytes del PDF en memoria"
output: "output_path (str) si se escribio a disco, o bytes del PDF si output_path es None"
tested: true
tests: ["guardar PDF a archivo", "retornar bytes del PDF", "PDF valido comienza con %PDF"]
test_file_path: "python/functions/infra/pdf_save_test.py"
file_path: "python/functions/infra/pdf_save.py"
---
## Ejemplo
```python
# Guardar a disco
path = pdf_save(doc, "reporte.pdf")
print(f"PDF guardado en: {path}")
# Obtener bytes (para HTTP response o guardar en BD)
pdf_bytes = pdf_save(doc)
assert pdf_bytes[:4] == b"%PDF"
# En un endpoint web (ejemplo conceptual)
return Response(pdf_save(doc), content_type="application/pdf")
```
## Notas
Llama a `fpdf2.output()`. Si el documento usa `alias_nb_pages()` (activado por `pdf_add_header_footer` con `page_numbers=True`), fpdf2 reemplaza el alias con el total real de paginas al serializar. Para retornar bytes, `output()` retorna un `bytearray` que se convierte a `bytes` para compatibilidad con APIs que esperan `bytes`.