DavidV DavidV - 1 year ago 130
Python Question

Missing letter č in ReportLab pdf created with Python 3.4

A couple of days ago I started to use ReportLab with Python34. It's pretty nice package but I have one big problem that I don't know how to overcome.

Could someone check my code and help me get over this? The problem is connected with letter č in Slovenian language. In the title there is no problem, but later in pdf file I cannot see that letter.

My code is below:

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf'))

styles = getSampleStyleSheet()

Title = "Izračun pokojnine"
bogustext =("""ččččččččččččččččččč""")

def myPage(canvas, doc):
canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)

def go():
doc = SimpleDocTemplate("phello.pdf")
Story = [Spacer(1,2*inch)]
style = styles["Normal"]
p = Paragraph(bogustext, style)
doc.build(Story, onFirstPage=myPage)


When I make pdf file I get this:
enter image description here

Why there is a difference between letter č in title and text?

Thanks in advance!

Best regards, David

Answer Source

The problem is that in the title you are using Vera as the font, in the text you are using the default font used by Reportlab which is Times-Roman (if I remember correctly).

The blackboxes you're seeing indicate that the current font (Times-Roman) doesn't have a symbol for the character you are trying to display. So to fix it you will have to change the font of the text to a font that does contain a symbol for č. One way to do this is by creating a new style Like this:


In some cases it might be easier to replace the missing symbols with the symbol form a fallback font in which case you might want to check out this answer I posted earlier this year.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download