使用python3.4创建的ReportLab pdf中缺少字母č

2024-09-28 03:21:15 发布

您现在位置:Python中文网/ 问答频道 /正文

几天前,我开始在Python34上使用ReportLab。这是一个很好的包裹,但我有一个大问题,我不知道如何克服。在

有人能帮我查一下密码吗?这个问题与斯洛文尼亚语中的字母č有关。在标题没有问题,但后来在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文件时,我得到了: enter image description here

为什么字母č在标题和正文中有区别?在

提前谢谢!在

谨致问候,大卫


Tags: fromimport标题docpdf字母pagecanvas
1条回答
网友
1楼 · 发布于 2024-09-28 03:21:15

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

您看到的黑匣子表明当前字体(Times-Roman)没有您要显示的字符的符号。所以要解决这个问题,你必须将文本的字体改为一个包含c符号的字体。一种方法是创建如下新样式:

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

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

相关问题 更多 >

    热门问题