在reportlab中创建和保存pdf会返回错误

时间:2015-10-28 18:13:05

标签: python pdf reportlab

我正在尝试使用reportlab创建一个pdf,但我一直收到错误,理想情况下我想将创建的pdf保存到特定目录,但这仅用于测试,而save函数只将其保存到当前工作目录

import os
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4

folder_path = "/home/ro/A Python Scripts/dest_test/"
folder_name = os.path.basename(folder_path)
pdf_name = folder_name + '.py'

def generate_pdf(folder_paths, folder_names, speedy_share_links):
    c = canvas.Canvas(folder_names)

    c.drawString(100,780,folder_names)
    c.drawString(100,750,speedy_share_links)
    c.save()

generate_pdf(folder_path, folder_name, "hiya")

我收到以下错误

Traceback (most recent call last):
  File "pdf.py", line 16, in <module>
    generate_pdf(folder_path, folder_name, "hiya")
  File "pdf.py", line 14, in generate_pdf
    c.save()
  File "/usr/lib/python2.7/dist-packages/reportlab/pdfgen/canvas.py", line 1209, in save
    self._doc.SaveToFile(self._filename, self)
  File "/usr/lib/python2.7/dist-packages/reportlab/pdfbase/pdfdoc.py", line 216, in SaveToFile
    f = open(filename, "wb")
IOError: [Errno 2] No such file or directory: u''

1 个答案:

答案 0 :(得分:0)

您的路径有一个空的基本名称,这就是错误显示空字符串的原因。

替换行:

folder_path = "/home/ro/A Python Scripts/dest_test/"

使用以下行:

folder_path = "/home/ro/A Python Scripts/dest_test/foobar.pdf"

,您的程序将在当前目录中生成foobar.pdf