fanny
fanny

Reputation: 1441

Inserting non-ASCII characters into pdf with Python reportlab

My code is supposed to insert a text into a pdf template. The variable text is

text = u"Gęślą JaźńZażółć"

First, I create an empty pdf, then a Canvas instance with the file as a parameter, then incorporate text with canvas_instance.drawString(10, 10, text.decode('utf-8') and save it. And my original pdf file indeed contains a text in a right position - but I get ASCII characters + neat black squares instead of non-ASCII letters. I have tried setting font to 'Times-Bold' and 'Helvetica' but they don't seem to work, either. I have a coding declaration in my python file, too:

# -*- coding: utf-8 -*-

How would I be able to insert text into a pdf template?

Python: 2.7 libs: reportlab, pyPdf ubuntu 14

Upvotes: 0

Views: 2282

Answers (1)

fanny
fanny

Reputation: 1441

ok, a ttf supporting Polish solved this problem:

name = u"{} {}".format(unicode(mydata['firstname']), unicode(mydata['lastname']))   
pdfmetrics.registerFont(TTFont('Theano', '<path>/TheanoOldStyle-Regular.ttf'))

can.setFont('Theano', 16)

can.drawString(175, 400, name.decode('utf-8'))

Upvotes: 1

Related Questions