【问题标题】:Provide tab title with reportlab generated pdf提供带有 reportlab 生成的 pdf 的选项卡标题
【发布时间】:2015-04-01 10:28:05
【问题描述】:

这个问题真的很简单,但是我找不到任何关于它的数据。 当我使用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

【问题讨论】:

  • 你能提供任何网站的例子吗?
  • 例如:这里,文件名显示在标题biblioteca.org.ar/libros/211756.pdf中。此外,当使用 weasyprint 生成时,会处理 html 标题标签,所以我很肯定它是可行的

标签: python django pdf-generation httpresponse reportlab


【解决方案1】:

似乎谷歌浏览器根本不显示 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 中打开它会产生所需的结果:

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

【讨论】:

  • 谢谢!这似乎可以解决问题。现在我应该尝试将其更改为不使用画布与鸭嘴兽一起使用
  • 关于文档中提到的 setTitle .. 它甚至没有解释它有什么效果,也没有在你不使用画布时提供帮助
【解决方案2】:

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

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

我们可以设置文档的标题

document.title = 'Sample Title'

【讨论】:

    【解决方案3】:

    我意识到这是一个老问题,但对于任何使用 SimpleDocTemplate 的人来说,我都会回答。 title 属性可以在 SimpleDocTemplate 的构造函数中设置为 kwarg。例如

    doc = SimpleDocTemplate(pdf_bytes, title="my_pdf_title")
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        除了别人说的,你可以用

        Canvas.setTitle("yourtitle")
        

        在 chrome 中显示良好。

        【讨论】:

          猜你喜欢
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-24
          • 1970-01-01
          • 2014-04-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多