Specify a tab title using reportlab generated pdf

This question is very simple, but I can not find any data on it. When I create a pdf file with reportlab, passing httpresponse as a file, browsers configured to display files display pdf correctly. However, the tab title remains "(anonymous) 127.0.0.1/whatnot", which looks ugly to the user.

Since most sites are capable of displaying a suitable title in some way, I think this is doable ... Is there any header parameter that I can pass to pdf? Or some kind of heading for an answer? 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 
+11
python django pdf-generation reportlab
source share
4 answers

It seems that Google Chrome does not display PDF names at all. I tested the link in your comment ( biblioteca.org.ar ) and it displays in Firefox as "211756.pdf", it seems that there is an empty title and then Firefox just displays the file name instead of the full URL.

I reproduced the same behavior 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 in Firefox gives the desired result:

I found out about setTitle in the setTitle User Guide . It is listed on page 16. :)

+11
source share

I also searched for this, and I found this in the source code.

ReportLab / IDC /ReportLab/planters/doctemplate.py @line - 467

We can set the title of the document with

 document.title = 'Sample Title' 
+1
source share

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

0
source share

In addition to what others have said, you can use

 Canvas.setTitle("yourtitle") 

which displays well in Chrome.

0
source share

All Articles