报告实验室功能不生成pdf文件

时间:2017-02-15 09:32:36

标签: python python-3.x pdf-generation reportlab

我试图在jpg图像上绘制字符串并使用reportlab将其设为pdf。 当我这样做的时候会起作用:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import landscape, A4, letter
from reportlab.lib.units import cm, inch


file = 'test.pdf'
name = 'Nita Alexandru Stefan'
year = '1993'

c = canvas.Canvas(file, pagesize=landscape(letter))
c.drawImage('cert.jpg', 0*cm, 0*cm, 29*cm, 22*cm)
c.setFont("Times-Roman", 12)
c.setFillColorRGB(0.2578125, 0.2265625, 0.23046875)
c.drawString(500, 374, name)
c.drawString(429, 359, year)
c.showPage()
c.save()

我想创建一个函数并使用dict.get(),当我尝试这个时它没有生成任何东西:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import landscape, A4, letter
from reportlab.lib.units import cm, inch

data = dict(name='Nita Alexandru Stefan', year='1993')


def generate_pdf(data, canvas):
    file = 'test.pdf'
    c = canvas.Canvas(file, pagesize=landscape(letter))
    c.drawImage('cert.jpg', 0*cm, 0*cm, 29*cm, 22*cm)
    c.setFont("Times-Roman", 12)
    c.setFillColorRGB(0.2578125, 0.2265625, 0.23046875)
    c.drawString(500, 374, data.get('name', ""))
    c.drawString(429, 359, data.get('year', ""))
    c.showPage()
    c.save()

为什么它没有生成任何内容,也没有更改之前创建的文件?

1 个答案:

答案 0 :(得分:1)

好像你没有打电话给generate_pdf function

致电:

generate_pdf(data, canvas)
函数generate_pdf结束后

像:

def generate_pdf(data, canvas):
    file = 'test.pdf'
    c = canvas.Canvas(file, pagesize=landscape(letter))
    c.drawImage('cert.jpg', 0*cm, 0*cm, 29*cm, 22*cm)
    c.setFont("Times-Roman", 12)
    c.setFillColorRGB(0.2578125, 0.2265625, 0.23046875)
    c.drawString(500, 374, data.get('name', ""))
    c.drawString(429, 359, data.get('year', ""))
    c.showPage()
    c.save()

generate_pdf(data, canvas)