Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode multi_cell line wrap breaks after parenthesis #118

Closed
davidnoyes opened this issue Mar 21, 2021 · 2 comments
Closed

Unicode multi_cell line wrap breaks after parenthesis #118

davidnoyes opened this issue Mar 21, 2021 · 2 comments
Labels

Comments

@davidnoyes
Copy link

I'm having an issue when using multi_cell with a unicode font, it seems like I get corrupted characters when a line break occurs and the line contains a parenthesis before the line break. This doesn't occur when I use pdf.write.

Python: 3.9.2
fpdf2: 2.3.1

I've been able to reproduce the issue with a sample project:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()

# Add a DejaVu Unicode font (uses UTF-8)
# Supports more than 200 languages. For a coverage status see:
# http://dejavu.svn.sourceforge.net/viewvc/dejavu/trunk/dejavu-fonts/langcover.txt
pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
pdf.set_font('DejaVu', '', 14)

text = """

Line wrap : lorem () dolor sit amet, consectetur sit amet adipiscing elit et dolore
No wrap : ipsum () dolor sit amet, consectetur sit amet adipiscing
Parenthesis after line break : ipsum dolor sit amet, consectetur adipiscing et dolore elit () lorem

"""

pdf.set_font('helvetica', '', 18)

pdf.write(8, "using pdf.multi_cell with built in font")

pdf.ln(0)

pdf.multi_cell(w=0, h=8, txt=text, ln=1)

pdf.ln(8)

pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
pdf.set_font('DejaVu', '', 14)

pdf.write(8, "using pdf.multi_cell with unicode font")

pdf.ln(0)

pdf.multi_cell(w=0, h=8, txt=text, ln=1)

pdf.ln(8)

pdf.write(10, "using pdf.write, line by line with unicode font")
for line in text.split('\n'):
    pdf.write(8, line)
    pdf.ln(8)

pdf.write(8, "using pdf.write for whole literal with unicode font")
pdf.write(8, text)

pdf.ln(8)

pdf.output("unicode.pdf")

The code outputs this pdf:
unicode.pdf

Any help you could provide would be greatly appreciated. Thanks.

@Lucas-C
Copy link
Member

Lucas-C commented Mar 22, 2021

This is indeed a bug. It seems to happen only in justified mode (align='J') with unicode fonts.
There is some special "manual" word spacing in that case:
https://github.com/PyFPDF/fpdf2/blob/master/fpdf/fpdf.py#L1209

@Lucas-C
Copy link
Member

Lucas-C commented Mar 22, 2021

This bug will be solved by #119

Thanks for reporting it @raspberrypython !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants