[英]Provide tab title with reportlab generated pdf
This question is really simple, but I can't find any data on it.这个问题真的很简单,但是我找不到任何有关它的数据。 When I generate a pdf with reportlab, passing the httpresponse as a file, browsers that are configured to show files display the pdf correctly.
当我使用 reportlab 生成 pdf 并将 httpresponse 作为文件传递时,配置为显示文件的浏览器会正确显示 pdf。 However, the title of the tab remains "(Anonymous) 127.0.0.1/whatnot", which is kinda ugly for the user.
但是,选项卡的标题仍然是“(匿名)127.0.0.1/whatnot”,这对用户来说有点难看。
Since most sites are able to somehow display an appropiate title, I think it's doable... Is there some sort of title parameter that I can pass to the pdf?由于大多数网站都能够以某种方式显示适当的标题,我认为这是可行的...是否有某种标题参数可以传递给 pdf? Or some header for the response?
或者一些响应头? This is my code:
这是我的代码:
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
Seems that Google Chrome doesn't display the PDF titles at all.似乎 Google Chrome 根本不显示 PDF 标题。 I tested the link in your comment ( biblioteca.org.ar ) and it displays in Firefox as " - 211756.pdf", seems there's an empty title and Firefox then just displays the filename instead of the full URL path.
我测试了您评论中的链接( biblioteca.org.ar ),它在 Firefox 中显示为“ - 211756.pdf”,似乎有一个空标题,Firefox 然后只显示文件名而不是完整的 URL 路径。
I reproduced the same behaviour using this piece of code:我使用这段代码重现了相同的行为:
from reportlab.pdfgen import canvas
c = canvas.Canvas("hello.pdf")
c.setTitle("hello stackoverflow")
c.drawString(100, 750, "Welcome to Reportlab!")
c.save()
Opening it in Firefox yields the needed result:在 Firefox 中打开它会产生所需的结果:
I found out about setTitle
in ReportLab's User Guide .我在ReportLab 的用户指南中发现了有关
setTitle
的信息。 It has it listed on page 16. :)它列在第 16 页。:)
I was also looking for this and I found this in the source code.我也在寻找这个,我在源代码中找到了这个。
reportlab/src/reportlab/platypus/doctemplate.py @ line - 467 reportlab/src/reportlab/platypus/doctemplate.py @ line - 467
We can set the document's title by我们可以通过设置文档的标题
document.title = 'Sample Title'
如果您使用的是 trml2pdf,则需要在模板标签中添加“title”属性,即 <template title="Invoices" ...
In addition to what others have said, you can use除了其他人所说的,您还可以使用
Canvas.setTitle("yourtitle")
which shows up fine in chrome.这在 chrome 中显示得很好。
I realise this is an old question but dropping in an answer for anyone using SimpleDocTemplate
.我意识到这是一个老问题,但为任何使用
SimpleDocTemplate
人提供了答案。 The title
property can be set in constructor of SimpleDocTemplate
as a kwarg. title
属性可以在SimpleDocTemplate
构造函数中SimpleDocTemplate
为 kwarg。 eg例如
doc = SimpleDocTemplate(pdf_bytes, title="my_pdf_title")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.