ValueError when using Hyperlink in header of multipage PDF

The building of the document fails when I try to build a multipage-pdf with a table as a header containing a hyperlink paragraph.

Everything works fine if the pdf consists of only one page (i.e. the ‘main’ table has less then 30 lines in the reproducible example), or if the paragraph in the header table is not a hyperlink.

I don’t know, if i’m doing anything wrong or if it’s a bug in reportlab.

The error (“ValueError: redefining named object: ‘Annot.NUMBER1′”) is not really helpful.

Reproducible example:

from reportlab.lib.pagesizes import A4, portrait
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Paragraph, SimpleDocTemplate, Table


class HeaderFooterCanvas(Canvas):
    def __init__(self, *args, **kwargs):
        Canvas.__init__(self, *args, **kwargs)
        self.pages = []

    def showPage(self):
        self.pages.append(dict(self.__dict__))
        self._startPage()

    def save(self):
        for page in self.pages:
            self.__dict__.update(page)
            self.draw_canvas()
            Canvas.showPage(self)
        Canvas.save(self)

    def draw_canvas(self):
        self.saveState()
        tbl_data = [
            [Paragraph("<link href='https://reportlab.com/'>reportlab.com</link>")],
        ]

        f = Table(data=tbl_data)
        f.wrapOn(
            self, A4[0] - 5.0 * cm, A4[1] - 5.0 * cm
        )  # wrap the table to this width, height in case it spills
        f.drawOn(self, 2.5 * cm, A4[1] - 2.5 * cm)  # draw it on our pdf at x,y
        self.restoreState()


doc = SimpleDocTemplate(
    "Test.pdf", pagesize=portrait(A4), leftMargin=2.5 * cm, rightMargin=2.5 * cm
)

elements = []
table_data = [[" "]]

for i in range(40):
    table_data.append([f"Table line: {str(i)}"])

table = Table(table_data, repeatRows=1, colWidths="100%")
elements.append(table)

doc.multiBuild(elements, canvasmaker=HeaderFooterCanvas)

Traceback:

File "...DocumentsPython Scriptsreportlab_url_header_bug.py", line 50, in <module>
    doc.multiBuild(elements, canvasmaker=HeaderFooterCanvas)
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusdoctemplate.py", line 1180, in multiBuild
    self.canv.save()
  File "...DocumentsPython Scriptsreportlab_url_header_bug.py", line 19, in save
    self.draw_canvas()
  File "...DocumentsPython Scriptsreportlab_url_header_bug.py", line 33, in draw_canvas
    f.drawOn(self, 2.5 * cm, A4[1] - 2.5 * cm)  # draw it on our pdf at x,y
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusflowables.py", line 112, in drawOn
    self._drawOn(canvas)
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusflowables.py", line 93, in _drawOn
    self.draw()#this is the bit you overload
    ^^^^^^^^^^^
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypustables.py", line 2301, in draw
    self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight))
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypustables.py", line 2514, in _drawCell
    v.drawOn(self.canv,x,y)
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusflowables.py", line 112, in drawOn
    self._drawOn(canvas)
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusflowables.py", line 93, in _drawOn
    self.draw()#this is the bit you overload
    ^^^^^^^^^^^
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusparagraph.py", line 2003, in draw
    self.drawPara(self.debug)
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusparagraph.py", line 2584, in drawPara
    if f.link: _do_link_line(0, dx, ws, tx)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusparagraph.py", line 1517, in _do_link_line
    _doLink(tx, link, (t_off, y, t_off+textlen, y+leading))
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabplatypusparagraph.py", line 1506, in _doLink
    tx._canvas.linkURL(link, rect, relative=1, kind=kind)
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabpdfgencanvas.py", line 1276, in linkURL
    self._addAnnotation(ann)
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabpdfgencanvas.py", line 1281, in _addAnnotation
    self._doc.addAnnotation(name, annotation)
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabpdfbasepdfdoc.py", line 295, in addAnnotation
    self.Reference(annotation, self.annotationName(name))
  File "...AppDataLocalanaconda3envsvanilla_reportlabLibsite-packagesreportlabpdfbasepdfdoc.py", line 519, in Reference
    raise ValueError("redefining named object: "+repr(name))
ValueError: redefining named object: 'Annot.NUMBER1' 

New contributor

Mirco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật