Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions brazilfiscalreport/danfe/danfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,11 @@ def _draw_recipient_sender(self):
DanfeBasicField(
w=w_dest_end,
description="ENDEREÇO",
content=self.long_field(text=dest_end, limit=100),
content=self.long_field(
text=dest_end,
limit=w_dest_end,
font_size=self.get_font_size("FONT_SIZE_CONT", True),
),
pdf=self,
)
)
Expand Down Expand Up @@ -1036,7 +1040,11 @@ def _draw_delivery_location(self):
DanfeBasicField(
w=widths["line2"]["endereco"],
description="ENDEREÇO",
content=self.long_field(text=endereco, limit=100),
content=self.long_field(
text=endereco,
limit=widths["line2"]["endereco"],
font_size=self.get_font_size("FONT_SIZE_CONT", True),
),
pdf=self,
)
)
Expand Down Expand Up @@ -1321,15 +1329,24 @@ def _draw_shipping(self):
BaseFieldInfo(w=30, description="CNPJ / CPF", content=cnpj_cpf),
]

w_transp_mun = 69
w_transp_uf = 8
w_transp_ie = 30
w_transp_ender = block_transporte.w - w_transp_mun - w_transp_uf - w_transp_ie

fields_line2 = [
BaseFieldInfo(
w=0,
description="ENDEREÇO",
content=self.long_field(text=ender, limit=50),
content=self.long_field(
text=ender,
limit=w_transp_ender,
font_size=self.get_font_size("FONT_SIZE_CONT", True),
),
),
BaseFieldInfo(w=69, description="MUNICÍPIO", content=municipio),
BaseFieldInfo(w=8, description="UF", content=uf),
BaseFieldInfo(w=30, description="INSCRIÇÃO ESTADUAL", content=ie),
BaseFieldInfo(w=w_transp_mun, description="MUNICÍPIO", content=municipio),
BaseFieldInfo(w=w_transp_uf, description="UF", content=uf),
BaseFieldInfo(w=w_transp_ie, description="INSCRIÇÃO ESTADUAL", content=ie),
]

fields_line3 = [
Expand Down
33 changes: 26 additions & 7 deletions brazilfiscalreport/xfpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,34 @@


class xFPDF(FPDF):
def long_field(self, text="", limit=0):
# Take care of long field
if not text:
def long_field(self, text="", limit=0, font_size=None, font_style=""):
if not text or limit <= 0:
return ""

while self.get_string_width(text) > limit:
# text = text[:-2] + u'\u2026'
text = "%s..." % text[:-4].rstrip()
return text
prev_font = (self.font_family, self.font_style, self.font_size_pt)

try:
if font_size:
self.set_font(self.default_font, font_style, font_size)

safe_limit = limit - 2

if self.get_string_width(text) <= safe_limit:
return text

words = text.split()
while words and self.get_string_width(" ".join(words) + "...") > safe_limit:
words.pop()

if words:
return " ".join(words) + "..."

while text and self.get_string_width(text + "...") > safe_limit:
text = text[:-1]
return text + "..." if text else ""

finally:
self.set_font(*prev_font)

def text_box(self, text, text_align, h_line, x, y, w, h, border=False):
if border:
Expand Down
Binary file modified tests/generated/danfe/danfe_overload.pdf
Binary file not shown.
Loading