简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM