Skip to content

Commit

Permalink
add DACTE generation
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianoMafraJunior committed Aug 1, 2024
1 parent 4ca2939 commit ae18543
Show file tree
Hide file tree
Showing 12 changed files with 2,340 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: sudo apt-get update --allow-releaseinfo-change && sudo apt-get install qpdf
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools fpdf2 phonenumbers python-barcode pytest pytest-cov
python -m pip install --upgrade pip setuptools fpdf2 phonenumbers python-barcode pytest qrcode pytest-cov
- name: Test
run: |
pytest --cov=./brazilfiscalreport --cov-report=xml --cov-branch --doctest-glob="docs/*.md"
Expand Down
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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)

## Beta Stage Notice 🚧

Expand All @@ -22,6 +23,7 @@ This library is currently in the beta stage of development. While it has many of
- [FPDF2](https://github.com/py-pdf/fpdf2) - PDF creation library for Python
- phonenumbers
- python-barcode
- qrcode

## To install 🔧

Expand Down Expand Up @@ -67,6 +69,25 @@ cce = DaCCe(xml=xml_content)
cce.output('cce.pdf')
```

### DACTE

```python
from brazilfiscalreport.dacte import Dacte

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

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

# Instantiate the DACTE object with the loaded XML content
dacte = Dacte(xml=xml_content)

# Save the generated PDF to a file
dacte.output('dacte.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 @@ -199,6 +220,75 @@ danfe = Danfe(xml_content, config=config)
danfe.output('output_danfe.pdf')
```

## Customizing DACTE

This section describes how to customize the PDF output of the DACTE using the DacteConfig 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 ``DanfeConfig``:

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=5, right=5, bottom=5, left=5)
```
- **Default**: top, right, bottom and left is 2 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 DacteConfig object with a full set of customizations:

```python
from brazilfiscalreport.dacte import (
Dacte,
DacteConfig,
FontType,
Margins,
)

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

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

# Create a configuration instance
config = DacteConfig(
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 Dacte instance
dacte = Dacte(xml_content, config=config)
dacte.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
17 changes: 17 additions & 0 deletions brazilfiscalreport/dacte/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .config import (
DacteConfig,
DecimalConfig,
FontType,
Margins,
ReceiptPosition,
)
from .dacte import Dacte

__all__ = [
"Dacte",
"DacteConfig",
"DecimalConfig",
"FontType",
"Margins",
"ReceiptPosition",
]
39 changes: 39 additions & 0 deletions brazilfiscalreport/dacte/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 = 2
right: Number = 2
bottom: Number = 2
left: Number = 2


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


class ReceiptPosition(Enum):
TOP = "top"
BOTTOM = "bottom"
LEFT = "left"


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

0 comments on commit ae18543

Please sign in to comment.