使用reportlab生成的pdf提供标签标题

时间:2022-06-12 09:46:32

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. However, the title of the tab remains "(Anonymous) 127.0.0.1/whatnot", which is kinda ugly for the user.

这个问题很简单,但我找不到任何数据。当我使用reportlab生成pdf时,将httpresponse作为文件传递,配置为显示文件的浏览器会正确显示pdf。但是,选项卡的标题仍为“(匿名)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? Or some header for the response? This is my code:

由于大多数网站能够以某种方式显示合适的标题,我认为这是可行的...是否有某种标题参数,我可以传递给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

2 个解决方案

#1


8  

Seems that Google Chrome doesn't display the PDF titles at all. 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.

似乎Google Chrome根本不显示PDF标题。我测试了你评论中的链接(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 *")
c.drawString(100, 750, "Welcome to Reportlab!")
c.save()

Opening it in Firefox yields the needed result: 使用reportlab生成的pdf提供标签标题

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

I found out about setTitle in ReportLab's User Guide. It has it listed on page 16. :)

我在ReportLab的用户指南中发现了setTitle。它已列在第16页。:)

#2


0  

If you are using trml2pdf, you will need to add the "title" attribute in the template tag, ie., <template title="Invoices" ...

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

#1


8  

Seems that Google Chrome doesn't display the PDF titles at all. 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.

似乎Google Chrome根本不显示PDF标题。我测试了你评论中的链接(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 *")
c.drawString(100, 750, "Welcome to Reportlab!")
c.save()

Opening it in Firefox yields the needed result: 使用reportlab生成的pdf提供标签标题

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

I found out about setTitle in ReportLab's User Guide. It has it listed on page 16. :)

我在ReportLab的用户指南中发现了setTitle。它已列在第16页。:)

#2


0  

If you are using trml2pdf, you will need to add the "title" attribute in the template tag, ie., <template title="Invoices" ...

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