Reportlab:鸭嘴兽中是否有可能拥有内部链接



我知道我可以在内部与画布链接,但我的整个文档都是用鸭嘴兽设置的。鸭嘴兽支持内部链接吗?如果没有,迁移到画布有多难?

提前感谢!

您可以使用段落内标记来创建锚点(<a>标签)和链接(<link>标签),如 ReportLab 2.6 用户手册 PDF 的第 6.3 节段落内标记(第 6 章,第 72 页)中所述,其中还包含以下示例:

This <a href="#MYANCHOR" color="blue">is a link to</a> an
anchor tag ie <a name="MYANCHOR"/><font color="green">here</font>.
This <link href="#MYANCHOR" color="blue" fontName="Helvetica">is
another link to</link> the same anchor tag.

一种受 https://www.blog.pythonlibrary.org/2014/03/10/reportlab-how-to-create-custom-flowables/启发的方法。

创建一个从 Flowable 继承的自定义类,该类可以添加到"故事"中

class flowable_bookmark(Flowable):
    def __init__(self, x=0, y=0, width=10, height=10, bookmark_name = "", text=""):
        Flowable.__init__(self)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.bookmark_name = bookmark_name
        self.text = text
    def draw(self):
        self.canv.drawString(self.x, self.y, self.text)
        self.canv.bookmarkPage(self.bookmark_name)

用法:

my_anchor = flowable_bookmark("MYANCHOR", text=" ")
self.story.append(my_anchor)

最新更新