简体   繁体   English

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

[英]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.

相关问题 使用reportLab生成的pdf大小 - Size of pdf generated with reportLab 使用 Reportlab 设置 PDF 的作者、标题和主题 - Set Author, Title, and Subject for PDF using Reportlab Unicode字符是Geraldo / ReportLab生成的PDF中的框 - Unicode characters are boxes in Geraldo/ReportLab generated PDF 是否可以在reportlab生成的pdf文档中插入pdf格式的图像? - Is it possible inserting pdf format image in a reportlab generated pdf document? 如何将生成的PDF与Reportlab附加到App Engine Python中的电子邮件中 - How to attach generated PDF with Reportlab to Email in App Engine Python Django / reportlab:将生成的pdf直接保存到AWS s3中的FileField - Django/reportlab: Save generated pdf directly to FileField in AWS s3 是否可以在ReportLab生成的PDF中包含OpenOffice Writer或MS Word数据? - Is it possible to include OpenOffice Writer or MS Word data in a ReportLab generated PDF? 使用reportlab生成的pdf文件,另存为纯文本 - pdf file generated using reportlab , getting saved as plain text 在reportlab中生成的pdf报告与原始文本数据不同 - Generated pdf report in reportlab is not similar to original text data 在ReportLab生成的PDF中包含base64编码的图像 - Including base64-encoded image in ReportLab-generated PDF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM