使用Python reportlab将非ASCII字符插入pdf(Inserting non

编程入门 行业动态 更新时间:2024-10-25 10:30:27
使用Python reportlab将非ASCII字符插入pdf(Inserting non-ASCII characters into pdf with Python reportlab)

我的代码应该将文本插入到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'))

更多推荐

本文发布于:2023-08-06 21:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   reportlab   Python   ASCII   Inserting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!