我的代码应该将文本插入到pdf模板中。 变量文本是
text = u"Gęślą JaźńZażółć"首先,我创建一个空的pdf,然后创建一个以文件作为参数的Canvas实例,然后将文本与canvas_instance.drawString(10, 10, text.decode('utf-8')并保存。我的原始pdf文件确实包含一个正确位置的文本 - 但我得到ASCII字符+整齐的黑色方块而不是非ASCII字母。我已经尝试将字体设置为'Times-Bold'和'Helvetica'但它们似乎不起作用,要么我的python文件中也有一个编码声明:
# -*- coding: utf-8 -*-我如何能够将文本插入到pdf模板中?
Python:2.7 libs:reportlab,pyPdf ubuntu 14
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
最满意答案
好的,支持波兰语的ttf解决了这个问题:
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'))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'))更多推荐
发布评论