Skip to content

Commit

Permalink
damdfe: fix qrcode, barcode size
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniospneto authored and CristianoMafraJunior committed Nov 7, 2024
1 parent be2b84c commit ca133b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
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)
58 changes: 38 additions & 20 deletions brazilfiscalreport/damdfe/damdfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ def extract_text(node: Element, tag: str) -> str:
class Damdfe(xFPDF):
def __init__(self, xml, config: DamdfeConfig = None):
super().__init__(unit="mm", format="A4")
config = config if config is not None else DamdfeConfig()
self.config = config if config is not None else DamdfeConfig()
self.set_margins(
left=config.margins.left, top=config.margins.top, right=config.margins.right
left=self.config.margins.left,
top=self.config.margins.top,
right=self.config.margins.right,
)
self.set_auto_page_break(auto=False, margin=config.margins.bottom)
self.set_auto_page_break(auto=False, margin=self.config.margins.bottom)
self.set_title("DAMDFE")
self.logo_image = config.logo
self.default_font = config.font_type.value
self.price_precision = config.decimal_config.price_precision
self.quantity_precision = config.decimal_config.quantity_precision
self.logo_image = self.config.logo
self.default_font = self.config.font_type.value
self.price_precision = self.config.decimal_config.price_precision
self.quantity_precision = self.config.decimal_config.quantity_precision

root = ET.fromstring(xml)
self.inf_adic = root.find(f"{URL}infAdic")
Expand Down Expand Up @@ -188,7 +190,7 @@ def _draw_header(self):
self.line(x_middle, y_margin, x_middle, y_margin + 88)

y_middle = y_margin + 25
self.line(x_margin, y_middle, x_middle, y_middle)
self.line(x_margin, y_middle, x_middle, y_middle) # Aqui
self.set_xy(x=x_margin, y=y_middle)
self.multi_cell(
w=100,
Expand All @@ -200,7 +202,7 @@ def _draw_header(self):
)

y_middle = y_margin + 28
self.line(x_margin, y_middle, x_middle, y_middle)
self.line(x_margin, y_middle, x_margin + page_width - 0.5, y_middle)
self.set_font(self.default_font, "", 6)

self.draw_vertical_lines(
Expand Down Expand Up @@ -340,23 +342,39 @@ def _draw_header(self):
qr_code = extract_text(self.inf_mdfe_supl, "qrCodMDFe")

num_x = 140
num_y = 3
num_w = 28
num_h = 28
draw_qr_code(self, qr_code, 0, num_x, num_y, num_w, num_h)
num_y = 1
draw_qr_code(self, qr_code, 0, num_x, num_y, box_size=25, border=3)

svg_img_bytes = BytesIO()
Code128(self.key_mdfe, writer=SVGWriter()).write(svg_img_bytes)
self.image(svg_img_bytes, x=115, y=self.l_margin + 3 + 35, w=82, h=12.5)
w_options = {
"module_width": 0.3,
}
Code128(self.key_mdfe, writer=SVGWriter()).write(
fp=svg_img_bytes,
options=w_options,
text="",
)
self.set_font(self.default_font, "", 6.5)
self.set_xy(x=x_margin + 100, y=y_middle)
self.multi_cell(
w=100,
h=3,
text="CONTROLE DO FISCO",
border=0,
align="L",
)
margins_offset = {1: 8, 2: 8, 3: 7, 4: 7, 5: 6, 6: 6, 7: 5.5, 8: 5, 9: 4, 10: 4}
x_offset = margins_offset.get(self.config.margins.right)
self.image(
svg_img_bytes, x=x_middle + x_offset, y=self.t_margin + 32, w=86.18, h=17.0
)

self.set_font(self.default_font, "", 6.5)
self.set_xy(x=x_middle + 6, y=y_middle + 23)
self.set_xy(x=x_middle + 25, y=y_middle + 23)
self.multi_cell(
w=100,
h=3,
text="Chave de acesso para consulta de "
"autenticidade no site "
"mdfe-portal.sefaz.rs.gov.br ou da Sefaz",
text="Consulta em https://dfe-portal.svrs.rs.gov.br/MDFE/Consulta",
border=0,
align="L",
)
Expand Down Expand Up @@ -392,7 +410,7 @@ def _draw_header(self):
)

y_middle = y_margin + 35
self.line(x_margin, y_middle, x_margin + page_width - 0.5, y_middle)
self.line(x_margin, y_middle, x_middle, y_middle)
self.draw_vertical_lines(
x_start_positions=[24, 64],
y_start=y_middle,
Expand Down
Binary file modified tests/generated/damdfe/damdfe_default.pdf
Binary file not shown.
Binary file modified tests/generated/damdfe/damdfe_default_logo.pdf
Binary file not shown.
Binary file modified tests/generated/damdfe/damdfe_default_logo_margins.pdf
Binary file not shown.

0 comments on commit ca133b4

Please sign in to comment.