简体   繁体   中英

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

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:

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. 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.

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:

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

I was also looking for this and I found this in the source code.

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.

I realise this is an old question but dropping in an answer for anyone using SimpleDocTemplate . The title property can be set in constructor of SimpleDocTemplate as a kwarg. eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM