2

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'))

PAGE_HEIGHT=defaultPageSize[1]
PAGE_WIDTH=defaultPageSize[0]
styles = getSampleStyleSheet()

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

def myPage(canvas, doc):
    canvas.saveState()
    canvas.setFont('Vera',16)
    canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
    canvas.restoreState()

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

go()

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

B8vrede
  • 4,432
  • 3
  • 27
  • 40
DavidV
  • 513
  • 3
  • 6
  • 11

1 Answers1

2

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:

ParagraphStyle('MyNormal',
               parent=styles['Normal'],
               fontName='Vera')

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.

Community
  • 1
  • 1
B8vrede
  • 4,432
  • 3
  • 27
  • 40
  • 1
    Many many thanks to @B8vrede :) It works like a charm. Before reading your answer I almost got to the same solution by myself.... but I must admit that you helped me a lot :) Thanks again! – DavidV Aug 19 '16 at 17:29