在reportlab中,可以使用pdfgen.canvas.Canvas
的bookmarkPage
方法来添加书签。这个方法接收两个参数:要添加书签的页面对象和书签名称。
例如,如果你要在文档中添加一个名为"目录"的书签,并将其链接到第3页,你可以这样做:
from reportlab.pdfgen import canvas
# Create the PDF object, using the response object as its "file."
c = canvas.Canvas(response)
# Your code to draw things on the PDF. Here's where the PDF generation happens.
# ...
# Add bookmark for the table of contents to page 3.
c.bookmarkPage('目录',3)
# Close the PDF object cleanly, and we're done.
c.showPage()
c.save()
调用showPage()和save()方法后,生成的PDF文档中就会有一个书签,点击它将自动跳转到第3页