提供带有reportlab生成的pdf标签的标题

问题描述 投票:11回答:4

这个问题确实很简单,但是我找不到任何数据。当我使用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
python django pdf-generation httpresponse reportlab
4个回答
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()

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

我发现了setTitle中的ReportLab's User Guide。它在第16页上列出。:)


1
投票

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

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

我们可以通过以下方式设置文档标题:>

document.title = 'Sample Title'

0
投票

如果使用trml2pdf,则需要在模板标记中添加“ title”属性,即


0
投票

除了别人说的以外,您还可以使用

© www.soinside.com 2019 - 2024. All rights reserved.