Skip to content

Commit

Permalink
Merge pull request #34 from CristianoMafraJunior/damdfe
Browse files Browse the repository at this point in the history
Geração DAMDFE
  • Loading branch information
antoniospneto authored Nov 7, 2024
2 parents e8241eb + ca133b4 commit e56a827
Show file tree
Hide file tree
Showing 16 changed files with 1,399 additions and 9 deletions.
93 changes: 91 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Python library for generating Brazilian auxiliary fiscal documents in PDF from X
- DANFE - Documento Auxiliar da Nota Fiscal Eletrônica (NF-e)
- DACCe - Documento Auxiliar da Carta de Correção Eletrônica (CC-e )
- DACTE - Documento Auxiliar do Conhecimento de Transporte Eletrônico (CT-e)
- DAMDFE - Documento Auxiliar do Manifesto Eletrônico de Documentos Fiscais (MDF-e)

## Beta Stage Notice 🚧

Expand Down Expand Up @@ -95,6 +96,25 @@ dacte = Dacte(xml=xml_content)
dacte.output('dacte.pdf')
```

### DAMDFE

```python
from brazilfiscalreport.damdfe import Damdfe

# Path to the XML file
xml_file_path = 'damdfe.xml'

# Load XML Content
with open(xml_file_path, "r", encoding="utf8") as file:
xml_content = file.read()

# Instantiate the DAMDFE object with the loaded XML content
damdfe = Damdfe(xml=xml_content)

# Save the generated PDF to a file
damdfe.output('damdfe.pdf')
```

## Samples 📝

Some sample PDFs generated by our unit tests are available for viewing in the [tests/generated](https://github.com/Engenere/BrazilFiscalReport/tree/main/tests/generated) directory.
Expand Down Expand Up @@ -125,7 +145,7 @@ Here is a breakdown of all the configuration options available in ``DanfeConfig`
```python
config.margins = Margins(top=5, right=5, bottom=5, left=5)
```
- **Default**: top, right, bottom and left is 2 mm.
- **Default**: top, right, bottom and left is 5 mm.

3. **Receipt Position**
- **Type**: ``ReceiptPosition`` (Enum)
Expand Down Expand Up @@ -253,7 +273,7 @@ Here is a breakdown of all the configuration options available in ``DanfeConfig`
```python
config.margins = Margins(top=5, right=5, bottom=5, left=5)
```
- **Default**: top, right, bottom and left is 2 mm.
- **Default**: top, right, bottom and left is 5 mm.

3. **Font Type**
- **Type**: ``FontType`` (Enum)
Expand Down Expand Up @@ -296,6 +316,75 @@ dacte = Dacte(xml_content, config=config)
dacte.output('output_dacte.pdf')
```

## Customizing DAMDFE

This section describes how to customize the PDF output of the DAMDFE using the DamdfeConfig class. You can adjust various settings such as margins, fonts, and tax configurations according to your needs.

### Configuration Options

Here is a breakdown of all the configuration options available in ``DamdfeConfig``:

1. **Logo**
- **Type**: ``str``, ``BytesIO``, or ``bytes``
- **Description**: Path to the logo file or binary image data to be included in the PDF. You can use a file path string or pass image data directly.
- **Example**:
```python
config.logo = "path/to/logo.jpg" # Using a file path
```
- **Default**: No logo.


2. **Margins**
- **Type**: ``Margins``
- **Fields**: ``top``, ``right``, ``bottom``, ``left`` (all of type ``Number``)
- **Description**: Sets the page margins for the PDF document.
- **Example**:
```python
config.margins = Margins(top=10, right=10, bottom=10, left=10)
```
- **Default**: top, right, bottom and left is 5 mm.

3. **Font Type**
- **Type**: ``FontType`` (Enum)
- **Values**: ``COURIER``, ``TIMES``
- **Description**: Font style used throughout the PDF document.
- **Example**::
```python
config.font_type = FontType.COURIER
```
- **Default**: ``TIMES``

### Usage Example with Customization

Here’s how to set up a DamdfeConfig object with a full set of customizations:

```python
from brazilfiscalreport.damdfe import (
Damdfe,
DacteConfig,
FontType,
Margins,
)

# Path to the XML file
xml_file_path = 'mdf-e.xml'

# Load XML Content
with open(xml_file_path, "r", encoding="utf8") as file:
xml_content = file.read()

# Create a configuration instance
config = DamdfeConfig(
logo='path/to/logo.png',
margins=Margins(top=10, right=10, bottom=10, left=10),
font_type=FontType.TIMES
)

# Use this config when creating a Damdfe instance
damdfe = Damdfe(xml_content, config=config)
damdfe.output('output_dacte.pdf')
```

## Credits 🙌
This is a fork of the [nfe_utils](https://github.com/edsonbernar/nfe_utils) project, originally created by [Edson Bernardino](https://github.com/edsonbernar).

Expand Down
4 changes: 1 addition & 3 deletions brazilfiscalreport/dacte/dacte.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,9 @@ def _draw_header(self):
qr_code = extract_text(self.inf_cte_supl, "qrCodCTe")
x_offset = 88 # Ajuste se necessário
y_offset = 32 # Ajuste se necessário
width = 40
height = 40

# Chamada correta para o método
draw_qr_code(self, qr_code, y_margin_ret, x_offset, y_offset, width, height)
draw_qr_code(self, qr_code, y_margin_ret, x_offset, y_offset, box_size=38)

def _draw_recipient_sender(self, config):
self.mun_ini = extract_text(self.ide, "xMunIni")
Expand Down
10 changes: 6 additions & 4 deletions brazilfiscalreport/dacte/generate_qrcode.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import qrcode


def draw_qr_code(self, qr_code_data, y_margin_ret, x_offset, y_offset, width, height):
def draw_qr_code(
self, qr_code_data, y_margin_ret, x_offset, y_offset, box_size=10, border=1
):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=1,
box_size=box_size,
border=border,
)
qr.add_data(qr_code_data)
qr.make(fit=True)
Expand All @@ -17,4 +19,4 @@ def draw_qr_code(self, qr_code_data, y_margin_ret, x_offset, y_offset, width, he
num_x = y_margin_ret + x_offset
num_y = self.t_margin + y_offset

self.image(qr_img_bytes, x=num_x + 1, y=num_y + 1, w=width - 2, h=height - 2)
self.image(qr_img_bytes, x=num_x + 1, y=num_y + 1, w=box_size, h=box_size)
10 changes: 10 additions & 0 deletions brazilfiscalreport/damdfe/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .config import DamdfeConfig, DecimalConfig, FontType, Margins
from .damdfe import Damdfe

__all__ = [
"Damdfe",
"DamdfeConfig",
"DecimalConfig",
"FontType",
"Margins",
]
32 changes: 32 additions & 0 deletions brazilfiscalreport/damdfe/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from dataclasses import dataclass, field
from enum import Enum
from io import BytesIO
from numbers import Number
from typing import Union


class FontType(Enum):
COURIER = "Courier"
TIMES = "Times"


@dataclass
class Margins:
top: Number = 5
right: Number = 5
bottom: Number = 5
left: Number = 5


@dataclass
class DecimalConfig:
price_precision: int = 4
quantity_precision: int = 4


@dataclass
class DamdfeConfig:
logo: Union[str, BytesIO, bytes] = None
margins: Margins = field(default_factory=Margins)
decimal_config: DecimalConfig = field(default_factory=DecimalConfig)
font_type: FontType = FontType.TIMES
Loading

0 comments on commit e56a827

Please sign in to comment.