Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BrazilFiscalReport fixes #101

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ Nfe(infNFe=Tnfe.InfNfe(ide=None, emit=Tnfe.InfNfe.Emit(CNPJ='59594315000157', CP
["Element '{http://www.portalfiscal.inf.br/nfe}infNFe': The attribute 'versao' is required but missing.", "Element '{http://www.portalfiscal.inf.br/nfe}infNFe': The attribute 'Id' is required but missing." [...]
```

Assinar o XML de uma nota usando a lib [erpbrasil.assinatura](https://github.com/erpbrasil/erpbrasil.assinatura) (funciona com os outros documentos eletrônicos também)
```
>>> # Assinar o XML de uma nota:
>>> with open(path_to_your_pkcs12_certificate, "rb") as pkcs12_buffer:
pkcs12_data = pkcs12_buffer.read()
>>> signed_xml = nfe.sign_xml(xml, pkcs12_data, cert_password, nfe.NFe.infNFe.Id)
```

Imprimir o DANFE usando a lib [BrazilFiscalReport](https://github.com/Engenere/BrazilFiscalReport) ou a lib [erpbrasil.edoc.pdf](https://github.com/erpbrasil/erpbrasil.edoc.pdf) (futuramente BrazilFiscalReport deve imprimir o pdf de outros documentos eletrônicos também; erpbrasil.edoc.pdf é uma lib mais 'legacy')
```
>>> # Imprimir o pdf de uma nota usando BrazilFiscalReport:
>>> pdf_bytes = nfe.to_pdf()
>>> # Imprimir o pdf de uma nota usando erpbrasil.edoc.pdf:
>>> pdf_bytes = nfe.to_pdf(engine="erpbrasil.edoc.pdf")
>>> # Ou então para imprimir e assinar junto:
>>> pdf_bytes = nfe.to_pdf(
pkcs12_data=cert_data,
pkcs12_password=cert_password,
doc_id=nfe.NFe.infNFe.Id,
)
```

**NFS-e padrão nacional**
```python
>>> # Ler uma NFS-e:
Expand Down
54 changes: 18 additions & 36 deletions nfelib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,38 +126,6 @@ def sign_xml(
xml_etree = etree.fromstring(xml.encode("utf-8"))
return Assinatura(certificate).assina_xml2(xml_etree, doc_id)

@classmethod
def xmls_to_pdf(
self,
xml_list: List,
engine: str = "brazilfiscalreport",
image: Optional[str] = None, # (base64 image)
cfg_layout: str = "ICMS_IPI",
receipt_pos: str = "top",
) -> bytes:
"""Serialize a list of xmls strings (usually only one) to a pdf."""
xml_bytes_list = [
xml.encode() if isinstance(xml, str) else xml for xml in xml_list
]
if engine == "brazilfiscalreport":
try:
from brazilfiscalreport.pdf_docs import Danfe
except ImportError:
raise (RuntimeError("brazilfiscalreport package is not installed!"))
return bytes(
Danfe(
xmls=xml_bytes_list,
image=image,
cfg_layout=cfg_layout,
receipt_pos=receipt_pos,
).output(dest="S")
)
try:
from erpbrasil.edoc.pdf import base
except ImportError:
raise (RuntimeError("erpbrasil.edoc.pdf package is not installed!"))
return base.ImprimirXml.imprimir(string_xml=xml_bytes_list[0])

def to_xml(
self,
indent: str = " ",
Expand Down Expand Up @@ -212,9 +180,7 @@ def validate_xml(self, schema_path: Optional[str] = None) -> List:
def to_pdf(
self,
engine: str = "brazilfiscalreport",
image: Optional[str] = None, # (base64 image)
cfg_layout: str = "ICMS_IPI",
receipt_pos: str = "top",
config: Any = None, # (actually expects a DanfeConfig)
pkcs12_data: Optional[bytes] = None,
pkcs12_password: Optional[str] = None,
doc_id: Optional[str] = None,
Expand All @@ -223,7 +189,23 @@ def to_pdf(
xml = self.to_xml()
if pkcs12_data:
xml = self.sign_xml(xml, pkcs12_data, pkcs12_password, doc_id)
return self.xmls_to_pdf([xml.encode()], engine, image, cfg_layout, receipt_pos)

if engine == "brazilfiscalreport":
try:
from brazilfiscalreport.danfe import Danfe
except ImportError:
raise (RuntimeError("the BrazilFiscalReport package is not installed!"))
return bytes(
Danfe(
xml=xml,
config=config,
).output(dest="S")
)
try:
from erpbrasil.edoc.pdf import base
except ImportError:
raise (RuntimeError("the erpbrasil.edoc.pdf package is not installed!"))
return base.ImprimirXml.imprimir(string_xml=xml)

# this was an attempt to keep the signature inside the
# binding before serializing it again. But at the moment it fails
Expand Down
2 changes: 1 addition & 1 deletion tests/fingerprint.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"nfe": [
"https://www.nfe.fazenda.gov.br/portal/listaConteudo.aspx?tipoConteudo=BMPFMBoln3w=",
"35ac8b0887d877858edcedc5748e132c"
"9eb5a858cab3085819274edddd7802b6"
],
"cte": [
"https://www.cte.fazenda.gov.br/portal/listaConteudo.aspx?tipoConteudo=0xlG1bdBass=",
Expand Down
Loading