【发布时间】:2015-02-28 04:44:54
【问题描述】:
我是 reportlab lib 的新手,我正在学习它同时从事大学项目。
我在wxpython 中创建了一个桌面应用程序,它可以将数据保存为 PDF。
我想在我的 pdf 中添加 2 行。其中行以名为名称的用户输入开始,然后是一些单词,再次在第二行是一些单词,用户名,然后是一些单词......
我尝试使用一些 Paragraph 和 canvas 方法和类,但无法获得所需的输出。
期望的输出:
Alex 正在从事大学项目。
reportlab 是非常好的库,Alex 喜欢它。
我的代码:
import os
import reportlab
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.pdfmetrics import registerFontFamily
from reportlab.pdfbase.ttfonts import TTFont
# Registered font family
pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf'))
pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf'))
pdfmetrics.registerFont(TTFont('VeraIt', 'VeraIt.ttf'))
pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf'))
# Registered fontfamily
registerFontFamily('Vera',normal='Vera',bold='VeraBd',italic='VeraIt',boldItalic='VeraBI')
# Output pdf file name.
can = canvas.Canvas("Bold_Trail.pdf", pagesize=A4)
# Setfont for whole pdf.
can.setFont('Vera', 12)
# student name variable.
student_name ="Alex"
# Content.
line1 = " is working on college project."
line2 = "Reportlab is very good lib, "
line3 = " liked it.<br>"
# Joining whole content together.
content = "<strong>" + student_name + "</strong>" + line1
content2 = line2 + "<strong>"+student_name + "</strong>" + line3
# drawString location calculation.
x = 0; y = 8.5 * 72
# First string.
can.drawString(x,y, content)
y = y - 72
# Second String.
can.drawString(x,y, content2)
# Create PDF.
can.save()
除了使用 XML 方法 <strong> 或 <b> 之外,还有其他方法在上述程序中不起作用吗?
单词应该保持在一行。
【问题讨论】:
标签: python canvas reportlab richtext