使用 Python 3.4 创建的 ReportLab pdf 中缺少字母 č

Missing letter č in ReportLab pdf created with Python 3.4

几天前,我开始将 ReportLab 与 Python34 一起使用。这是一个非常好的包,但我有一个大问题,我不知道如何克服。

有人可以检查我的代码并帮助我解决这个问题吗?问题与斯洛文尼亚语中的字母 č 有关。标题上没有问题,后来pdf文件里就看不到那个字母了

我的代码如下:

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

当我制作 pdf 文件时,我得到了这个:

为什么标题和正文中的字母 č 不一样?

提前致谢!

最好的问候,大卫

问题是您在标题中使用 Vera 作为字体,在文本中您使用 Reportlab 使用的默认字体 Times-Roman(如果我没记错的话)。

您看到的黑框表示当前字体 (Times-Roman) 没有您要显示的字符的符号。因此,要修复它,您必须将文本的字体更改为包含 č 符号的字体。一种方法是创建一种新样式,如下所示:

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

在某些情况下,用后备字体的符号替换缺失的符号可能更容易,在这种情况下,您可能需要查看 this answer I posted earlier this year.