簡體   English   中英

使用 reportlab 生成的 pdf 提供選項卡標題

[英]Provide tab title with reportlab generated pdf

這個問題真的很簡單,但是我找不到任何有關它的數據。 當我使用 reportlab 生成 pdf 並將 httpresponse 作為文件傳遞時,配置為顯示文件的瀏覽器會正確顯示 pdf。 但是,選項卡的標題仍然是“(匿名)127.0.0.1/whatnot”,這對用戶來說有點難看。

由於大多數網站都能夠以某種方式顯示適當的標題,我認為這是可行的...是否有某種標題參數可以傳遞給 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

似乎 Google Chrome 根本不顯示 PDF 標題。 我測試了您評論中的鏈接( biblioteca.org.ar ),它在 Firefox 中顯示為“ - 211756.pdf”,似乎有一個空標題,Firefox 然后只顯示文件名而不是完整的 URL 路徑。

我使用這段代碼重現了相同的行為:

from reportlab.pdfgen import canvas

c = canvas.Canvas("hello.pdf")
c.setTitle("hello stackoverflow")
c.drawString(100, 750, "Welcome to Reportlab!")
c.save()

在 Firefox 中打開它會產生所需的結果:

我在ReportLab 的用戶指南中發現了有關setTitle的信息 它列在第 16 頁。:)

我也在尋找這個,我在源代碼中找到了這個。

reportlab/src/reportlab/platypus/doctemplate.py @ line - 467

我們可以通過設置文檔的標題

document.title = 'Sample Title'

如果您使用的是 trml2pdf,則需要在模板標簽中添加“title”屬性,即 <template title="Invoices" ...

除了其他人所說的,您還可以使用

Canvas.setTitle("yourtitle")

這在 chrome 中顯示得很好。

我意識到這是一個老問題,但為任何使用SimpleDocTemplate人提供了答案。 title屬性可以在SimpleDocTemplate構造函數中SimpleDocTemplate為 kwarg。 例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM