使用reportlab生成的pdf提供选项卡标题

Alv*_*aro 11 python django pdf-generation reportlab httpresponse

这个问题很简单,但我找不到任何数据.当我使用reportlab生成pdf,将httpresponse作为文件传递时,配置为显示文件的浏览器会正确显示pdf.但是,选项卡的标题仍为"(匿名)127.0.0.1/whatnot",这对用户来说有点难看.

由于大多数网站能够以某种方式显示适当的标题,我认为这是可行的...是否有某种标题参数,我可以传递给PDF?或者响应的一些标题?这是我的代码:

def render_pdf_report(self, context, file_name):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename="{}"'.format(file_name)

    document = BaseDocTemplate(response, **self.get_create_document_kwargs())
    #  pdf generation code
    document.build(story)
    return response
Run Code Online (Sandbox Code Playgroud)

Igo*_*ist 11

似乎Google Chrome根本不显示PDF标题.我测试了你评论中的链接(biblioteca.org.ar),它在Firefox中显示为" - 211756.pdf",似乎有一个空标题,然后Firefox只显示文件名而不是完整的URL路径.

我使用这段代码重现了相同的行为:

from reportlab.pdfgen import canvas

c = canvas.Canvas("hello.pdf")
c.setTitle("hello stackoverflow")
c.drawString(100, 750, "Welcome to Reportlab!")
c.save()
Run Code Online (Sandbox Code Playgroud)

在Firefox中打开它会产生所需的结果:

setTitleReportLab的用户指南中发现了这一点.它已列在第16页.:)

  • 完成.document.title ='title'可以解决问题 (6认同)
  • 关于文档中提到的 setTitle .. 它甚至没有解释它有什么效果,也没有在您不使用画布时提供帮助 (2认同)

小智 5

我也在寻找这个,我在源代码中找到了这个。

reportlab/src/reportlab/platypus/doctemplate.py @ line - 467

我们可以通过设置文档的标题

document.title = 'Sample Title'
Run Code Online (Sandbox Code Playgroud)


Fer*_*min 5

我意识到这是一个老问题,但为任何使用SimpleDocTemplate. 该title属性可以在构造函数中设置SimpleDocTemplate为 kwarg。例如

doc = SimpleDocTemplate(pdf_bytes, title="my_pdf_title")
Run Code Online (Sandbox Code Playgroud)