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'
Mirco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.