Generación de PDF para representación impresa de CPE — Facturación Electrónica Perú
Project description
xfep-pdf
Generación de representación impresa en PDF para Comprobantes de Pago Electrónicos — Perú (SUNAT).
Parte del ecosistema XFEP. Depende de xfep-models.
Genera el formato A4 estándar SUNAT con QR incluido, a partir de cualquier documento xfep-models (Factura, Boleta, Notas de Crédito/Débito, Guía de Remisión).
Instalación
xfep-pdf soporta dos motores de renderizado. Instalá uno como extra:
# WeasyPrint (recomendado — mejor calidad tipográfica)
pip install "xfep-pdf[weasyprint]"
# ReportLab (fallback — no requiere dependencias del sistema)
pip install "xfep-pdf[reportlab]"
Uso
from xfep.pdf import PdfGenerator
from xfep.models import Invoice, Company
company = Company(
ruc="20123456789",
razon_social="MI EMPRESA S.A.C.",
direccion="Av. Principal 123, Lima",
# ... demás campos
)
invoice = Invoice(
# ... completar con datos del comprobante
)
gen = PdfGenerator()
pdf_bytes: bytes = gen.generate(
document=invoice,
company=company,
correlativo=42,
logo=None, # opcional: bytes PNG/JPEG
)
# Guardar o servir
with open("F001-00000042.pdf", "wb") as f:
f.write(pdf_bytes)
Documentos soportados
| Modelo | Label | Template |
|---|---|---|
Invoice |
FACTURA ELECTRÓNICA | invoice.html |
Boleta |
BOLETA DE VENTA ELECTRÓNICA | invoice.html (compartido) |
CreditNote |
NOTA DE CRÉDITO ELECTRÓNICA | credit_note.html |
DebitNote |
NOTA DE DÉBITO ELECTRÓNICA | debit_note.html |
DispatchGuide |
GUÍA DE REMISIÓN ELECTRÓNICA | dispatch.html |
Características
- Motor dual — WeasyPrint (prioritario) con fallback a ReportLab.
- QR embebido — Generado según especificación SUNAT (RUC, tipo, serie, correlativo, IGV, total, fecha, cliente).
- Templates Jinja2 — Personalizables;
autoescape=Truepor defecto. - Logo opcional — Acepta bytes PNG/JPEG; se embebe como base64.
- Cálculos incluidos — Subtotal, IGV (18%), ISC, ICBPER, total por línea y documento.
- Formato A4 estándar — Cumple lineamientos SUNAT para representación impresa.
API
PdfGenerator.generate
PdfGenerator().generate(
document: Invoice | Boleta | CreditNote | DebitNote | DispatchGuide,
company: Company,
*,
correlativo: int = 1,
logo: bytes | None = None,
) -> bytes
Retorna los bytes del PDF generado. Lanza ValueError si el tipo de documento no es soportado.
Helpers de QR
from xfep.pdf import build_qr_string, generate_qr_base64
qr_text = build_qr_string(invoice, company, correlativo=42, total_igv="180.00", total_venta="1180.00")
qr_b64 = generate_qr_base64(qr_text)
Stack
- Python >= 3.13
- Jinja2 >= 3.1
- segno >= 1.6 (generación de QR)
- xfep-models >= 0.1.0
- Extras:
weasyprint>=62.0oreportlab>=4.0 + pillow>=10.0 - Build: Hatchling
- Tests: pytest
Desarrollo
git clone https://github.com/Xpertik/xfep-pdf.git
cd xfep-pdf
python3.13 -m venv .venv
source .venv/bin/activate
pip install -e "../xfep-models"
pip install -e ".[dev,weasyprint]"
pytest -v
Licencia
Apache License 2.0 — ver LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xfep_pdf-0.1.0.tar.gz.
File metadata
- Download URL: xfep_pdf-0.1.0.tar.gz
- Upload date:
- Size: 50.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac477f67a79cf7f3a701df43e6c51b2ccde274b7e6cb2c992c204993851c8f68
|
|
| MD5 |
8b91bd58f112d4dcf2b8539bcf650f40
|
|
| BLAKE2b-256 |
566b4507f2b0c709592f0227564c245cfabb470e60b9c95da06291681b601e27
|
File details
Details for the file xfep_pdf-0.1.0-py3-none-any.whl.
File metadata
- Download URL: xfep_pdf-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b08170c4cb2a4a649d266554d91b3aeb5cd56d4b72bb5608218f1c86004defc
|
|
| MD5 |
2ec69e6ee6c2e37b61cb5c02d9bc8442
|
|
| BLAKE2b-256 |
668f1f0f0b91c59b2b3bc34e78c34af55c376f58ac71b600072e523b592df338
|