Skip to content

Commit 78d930c

Browse files
Merge pull request #140 from PedroJoMa/fix-dynamic-delimitation-fields
Danfe: Fix dynamically adjusts the character limit in the address, following the margin dimensions.
2 parents 20ee05d + 346fc6b commit 78d930c

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

brazilfiscalreport/danfe/danfe.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,11 @@ def _draw_recipient_sender(self):
891891
DanfeBasicField(
892892
w=w_dest_end,
893893
description="ENDEREÇO",
894-
content=self.long_field(text=dest_end, limit=100),
894+
content=self.long_field(
895+
text=dest_end,
896+
limit=w_dest_end,
897+
font_size=self.get_font_size("FONT_SIZE_CONT", True),
898+
),
895899
pdf=self,
896900
)
897901
)
@@ -1036,7 +1040,11 @@ def _draw_delivery_location(self):
10361040
DanfeBasicField(
10371041
w=widths["line2"]["endereco"],
10381042
description="ENDEREÇO",
1039-
content=self.long_field(text=endereco, limit=100),
1043+
content=self.long_field(
1044+
text=endereco,
1045+
limit=widths["line2"]["endereco"],
1046+
font_size=self.get_font_size("FONT_SIZE_CONT", True),
1047+
),
10401048
pdf=self,
10411049
)
10421050
)
@@ -1321,15 +1329,24 @@ def _draw_shipping(self):
13211329
BaseFieldInfo(w=30, description="CNPJ / CPF", content=cnpj_cpf),
13221330
]
13231331

1332+
w_transp_mun = 69
1333+
w_transp_uf = 8
1334+
w_transp_ie = 30
1335+
w_transp_ender = block_transporte.w - w_transp_mun - w_transp_uf - w_transp_ie
1336+
13241337
fields_line2 = [
13251338
BaseFieldInfo(
13261339
w=0,
13271340
description="ENDEREÇO",
1328-
content=self.long_field(text=ender, limit=50),
1341+
content=self.long_field(
1342+
text=ender,
1343+
limit=w_transp_ender,
1344+
font_size=self.get_font_size("FONT_SIZE_CONT", True),
1345+
),
13291346
),
1330-
BaseFieldInfo(w=69, description="MUNICÍPIO", content=municipio),
1331-
BaseFieldInfo(w=8, description="UF", content=uf),
1332-
BaseFieldInfo(w=30, description="INSCRIÇÃO ESTADUAL", content=ie),
1347+
BaseFieldInfo(w=w_transp_mun, description="MUNICÍPIO", content=municipio),
1348+
BaseFieldInfo(w=w_transp_uf, description="UF", content=uf),
1349+
BaseFieldInfo(w=w_transp_ie, description="INSCRIÇÃO ESTADUAL", content=ie),
13331350
]
13341351

13351352
fields_line3 = [

brazilfiscalreport/xfpdf.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,34 @@
22

33

44
class xFPDF(FPDF):
5-
def long_field(self, text="", limit=0):
6-
# Take care of long field
7-
if not text:
5+
def long_field(self, text="", limit=0, font_size=None, font_style=""):
6+
if not text or limit <= 0:
87
return ""
98

10-
while self.get_string_width(text) > limit:
11-
# text = text[:-2] + u'\u2026'
12-
text = "%s..." % text[:-4].rstrip()
13-
return text
9+
prev_font = (self.font_family, self.font_style, self.font_size_pt)
10+
11+
try:
12+
if font_size:
13+
self.set_font(self.default_font, font_style, font_size)
14+
15+
safe_limit = limit - 2
16+
17+
if self.get_string_width(text) <= safe_limit:
18+
return text
19+
20+
words = text.split()
21+
while words and self.get_string_width(" ".join(words) + "...") > safe_limit:
22+
words.pop()
23+
24+
if words:
25+
return " ".join(words) + "..."
26+
27+
while text and self.get_string_width(text + "...") > safe_limit:
28+
text = text[:-1]
29+
return text + "..." if text else ""
30+
31+
finally:
32+
self.set_font(*prev_font)
1433

1534
def text_box(self, text, text_align, h_line, x, y, w, h, border=False):
1635
if border:
-7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)