reportlab保存位置

2024-10-01 17:42:13 发布

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

我正在尝试将reportlab生成的PDF文件保存到特定位置。有可能吗?代码会将pdf创建到自己的目录中。在

def myFirstPage(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Bold',16)
    canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
    canvas.setFont('Times-Roman',9)
    canvas.restoreState()

def create_pdf(sometext):
    doc = SimpleDocTemplate("myfile.pdf")
    Story = [Spacer(1,2*inch)]
    style = styles["Normal"]
    bogustext = ("There is something: %s. " % sometext)
    p = Paragraph(bogustext, style)
    Story.append(p)
    Story.append(Spacer(1,0.2*inch))
    doc.build(Story, onFirstPage=myFirstPage)

Tags: docpdfstyledefpagecanvastimesstory
1条回答
网友
1楼 · 发布于 2024-10-01 17:42:13

是的,这是可能的。我建议使用^{}来建立路径。示例:

import os

def create_pdf(sometext):
    outfilename = "myfile.pdf"
    outfiledir = '/somedir'
    outfilepath = os.path.join( outfiledir, outfilename )
    doc = SimpleDocTemplate( outfilepath )
    # ...

相关问题 更多 >

    热门问题