【问题标题】:Generating a PDF Index with ReportLab使用 ReportLab 生成 PDF 索引
【发布时间】:2016-03-14 12:24:14
【问题描述】:

我正在通过 ReportLab 生成 PDF 文件,但我找不到任何关于如何生成链接到文件页面的索引的文档。 ReportLab 是否支持此类功能,或者有其他解决方案吗?

【问题讨论】:

    标签: python pdf reportlab


    【解决方案1】:

    经过大量时间搜索,我想出了一个anchor 解决方案。虽然我觉得这对我来说不是完美的解决方案,但我希望它可以帮助有需要的人。

    from reportlab.lib.styles import ParagraphStyle
    from reportlab.pdfbase.pdfmetrics import registerFont
    from reportlab.platypus import Paragraph, PageBreak, SimpleDocTemplate, Spacer
    
    registerFont(TTFont('Calibri', 'Calibri.ttf')) # Just some font imports
    registerFont(TTFont('Calibri-Bold', 'calibrib.ttf'))
    
    pH = ParagraphStyle(name = 'Header', fontName = 'Calibri-Bold', fontSize = 13, leftIndent = 20, firstLineIndent = -20, spaceBefore = 10, leading = 16)
    sH = ParagraphStyle(name = 'SubHeader', fontName = 'Calibri', fontSize = 12, leftIndent = 40, firstLineIndent = -20, spaceBefore = 5, leading = 16)
    
    doc = SimpleDocTemplate('Reports\\PDFname.pdf')
    story = [Spacer(1, 2 * inch)]
    
    story.append(Paragraph('<a href = page3.html#0>1. First Title</a>', pH)) # Linking the anchor to reference 0
    story.append(Paragraph('<a href = page3.html#1>1.1. First Subtitle</a>', sH)) # Linking the anchor to reference 1
    story.append(PageBreak())
    
    story.append(Paragraph('<a name = page3.html#0></a> 1. First Title', pH)) # Creating anchor with reference 0
    story.append(Paragraph('<a name = page3.html#1></a><br/> 1.1. First Subtitle', style)) # Creating anchor with reference 1
    
    doc.build(story)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2017-03-24
      • 2017-12-05
      • 1970-01-01
      • 2015-10-11
      相关资源
      最近更新 更多