danfse
Gerador da DANFSe (Documento Auxiliar da NFS-e Nacional) em PDF a partir do XML da NFS-e, em Python.
Port para Python da biblioteca PHP
guilhermecfviana/danfse-nacional
(MIT). Renderiza o leiaute oficial DANFSe v2.0 via HTML + CSS com
WeasyPrint, com QR Code de consulta pública.
Contexto: o serviço de geração de DANFSe (PDF) do gov.br foi descontinuado. Esta biblioteca gera o PDF localmente a partir do XML autorizado da nota.
Instalação
pip install danfse
Dependências de sistema (WeasyPrint)
O WeasyPrint precisa de bibliotecas nativas (Pango, cairo, gdk-pixbuf). Instale conforme o sistema operacional:
# Debian / Ubuntu
sudo apt-get install -y libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf-2.0-0 \
libffi-dev libcairo2 fonts-liberation
# macOS (Homebrew)
brew install pango gdk-pixbuf libffi
# Fedora
sudo dnf install pango gdk-pixbuf2 cairo liberation-sans-fonts
Para melhor fidelidade visual, instale as fontes Liberation Sans (metric-compatíveis com Arial). Veja a documentação oficial do WeasyPrint para detalhes por plataforma.
Uso
Python
from danfse import generate
# Autodetecta: caminho de arquivo, string XML ou bytes XML
pdf = generate("nota.xml")
with open("danfse.pdf", "wb") as fh:
fh.write(pdf)
Funções específicas e opções:
from danfse import generate_from_file, generate_from_string, Options
generate_from_file("nota.xml", Options(output="file", output_path="danfse.pdf"))
xml = open("nota.xml", "rb").read()
pdf = generate_from_string(xml, {"watermark": "cancelada"}) # dict também funciona
Opções (Options):
| campo | valores | descrição |
|---|---|---|
output |
"bytes" (padrão) | "file" |
também grava em output_path quando file |
output_path |
caminho | PDF de saída |
watermark |
None | "cancelada" | "substituida" |
marca d'água |
logo |
caminho de imagem | substitui o logo padrão da NFS-e |
footer_text |
texto | rodapé repetido em todas as páginas |
CLI
danfse nota.xml # gera nota.pdf
danfse nota.xml -o danfse.pdf
danfse nota.xml --watermark cancelada
Notas sobre o leiaute
- Notas em homologação (
tpAmb = 2) recebem marca d'água "HOMOLOGAÇÃO" e o aviso "NFS-e SEM VALIDADE JURÍDICA". - Grupos opcionais do leiaute (intermediário, destinatário, IBS/CBS) só são exibidos quando presentes no XML.
- O QR Code aponta para a consulta pública oficial:
https://www.nfse.gov.br/ConsultaPublica/?tpc=1&chave={chave}.
Desenvolvimento
pip install -e ".[dev]"
pytest
ruff check .
Licença
MIT. Veja LICENSE e NOTICE (créditos ao projeto original e aos dados/ativos de terceiros).
Metadata
Release files for danfse 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| danfse-0.1.0.tar.gz | 135.0 kB | Without provenance | Details for danfse-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| danfse-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for danfse-0.1.0-py3-none-any.whl |
Total release size: 268.4 kB
Release files / danfse-0.1.0.tar.gz
| Download URL | danfse-0.1.0.tar.gz |
|---|---|
| Size | 135.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0bceb8ce7fbcb7ebea4ededaf0ff1705eb5d077386b6a743506a923dd5582a87
|
|
BLAKE2b-256 checksum How to use checksums |
c949fd31584696dda02423f485312690c7cf67474cadb61a8496890820c68b2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / danfse-0.1.0-py3-none-any.whl
| Download URL | danfse-0.1.0-py3-none-any.whl |
|---|---|
| Size | 133.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c61a5447c89d92472563a674054ec51c3c76026a290cabad3586df4a20e9660e
|
|
BLAKE2b-256 checksum How to use checksums |
7d1e9574d09f98527cd45b7df1498096bf706340c7d639f487b3bee3fcdf0dd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for danfse 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.