DANFE
DANFE (Auxiliary Document of the Electronic Invoice) is a printed representation of the NF-e (Electronic Invoice) used in Brazil. It contains key details about the transaction, such as the seller, buyer, products, and taxes.
Basic Usage
from brazilfiscalreport.danfe import Danfe
# Path to the XML file
xml_file_path = 'nfe.xml'
# Load XML Content
with open(xml_file_path, "r", encoding="utf8") as file:
xml_content = file.read()
# Instantiate the DANFE object with the loaded XML content
danfe = Danfe(xml=xml_content)
danfe.output('output_danfe.pdf')
Customizing DANFE 🎨
This section describes how to customize the PDF output of the DANFE using the DanfeConfig 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:
Logo
- Type:
str,BytesIO, orbytes - 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:
- Default: No logo.
Margins
- Type:
Margins - Fields:
top,right,bottom,left(all of typeNumber) - Description: Sets the page margins for the PDF document.
- Example:
- Default: top, right, bottom, and left are set to 5 mm.
Font Type
- Type:
FontType(Enum) - Values:
COURIER,TIMES - Description: Font style used throughout the PDF document.
- Example:
- Default:
TIMES
Font Size
- Type:
FontSize(Enum) - Values:
BIG,SMALL - Description: Font size used throughout the PDF document.
- Example:
- Default:
SMALL
Receipt Position
- Type:
ReceiptPosition(Enum) - Values:
TOP,BOTTOM,LEFT - Description: Position of the receipt section in the DANFE.
- Example:
- Default:
TOPwhen portrait,LEFTwhen landscape orientation.
Note
In landscape orientation, the receipt position is far left; customization is not permitted.
Decimal Configuration
- Type:
DecimalConfig - Fields:
price_precision,quantity_precision(bothint) - Description: Defines the number of decimal places for prices and quantities.
- Example:
- Default:
4
Tax Configuration
- Type:
TaxConfiguration(Enum) - Values:
STANDARD_ICMS_IPI,ICMS_ST,WITHOUT_IPI - Description: Specifies which tax fields to display.
- Example:
- Default:
STANDARD_ICMS_IPI
Warning
This feature is not yet implemented.
Invoice Display
- Type:
InvoiceDisplay(Enum) - Values:
DUPLICATES_ONLY,FULL_DETAILS - Description: Controls the level of detail in the invoice section of the DANFE.
- Example:
- Default:
FULL_DETAILS
Display PIS COFINS
- Type:
Bool - Values:
True,False - Description: Whether or not to display PIS and COFINS taxes in the DANFE totals.
- Example:
- Default:
False
Line break in supplementary information
- Type:
Bool - Values:
True,False - Description: Break the line using";" in the supplementary information (infCpl) of the DANFE.
- Example:
- Default:
False
Product Description Config
- Type:
ProductDescriptionConfig - Fields:
display_branch(bool),display_anp(bool),display_anvisa(bool),branch_info_prefix(str),display_additional_info(bool) - Description: Controls what additional information is displayed in the product description column of the DANFE.
- Example:
- Default:
Watermark Cancelled
- Type:
bool - Description: When set to
True, displays a "CANCELADA" watermark on the DANFE for cancelled invoices. For XML files without theprotNFetag, a "SEM VALOR FISCAL" watermark is displayed regardless of this setting. - Example:
- Default:
False
Usage Example with Customization
Here’s how to set up a DanfeConfig object with a full set of customizations::
from brazilfiscalreport.danfe import (
Danfe,
DanfeConfig,
DecimalConfig,
FontType,
InvoiceDisplay,
Margins,
ProductDescriptionConfig,
ReceiptPosition,
TaxConfiguration,
)
# Path to the XML file
xml_file_path = 'nfe.xml'
# Load XML Content
with open(xml_file_path, "r", encoding="utf8") as file:
xml_content = file.read()
# Create a configuration instance
config = DanfeConfig(
logo='path/to/logo.png',
margins=Margins(top=10, right=10, bottom=10, left=10),
receipt_pos=ReceiptPosition.BOTTOM,
decimal_config=DecimalConfig(price_precision=2, quantity_precision=2),
tax_configuration=TaxConfiguration.ICMS_ST,
invoice_display=InvoiceDisplay.FULL_DETAILS,
font_type=FontType.TIMES,
display_pis_cofins=True,
product_description_config=ProductDescriptionConfig(
display_branch=True,
display_additional_info=True,
),
)
# Use this config when creating a Danfe instance
danfe = Danfe(xml_content, config=config)
danfe.output('output_danfe.pdf')