Internal clickable links with dynamic page number

88 views
Skip to first unread message

Jeffery Liao

unread,
Jul 12, 2023, 5:33:55 AM7/12/23
to reportlab-users
Hi,
I'm trying to make multiple link with dynamic page numbers (not table of content).

for example "please refer to page.x for more details".

The x will replaced by actuall page number where my flowables at, usually Paragraph , and is clickable which jumps to the page.

In the afterFlowable function, I was able to acquire page number of the target Paragraph and link Paragraph by style name, but unable to change the x to page number.

How do I replace the x to page number dynamically and how to build multiple links jumps to different pages? Any tips are appreciated.

code here:
```
class MyDocTemplate(BaseDocTemplate):

    def __init__(self, filename, **kw):
        BaseDocTemplate.__init__(self, filename, **kw)
        template = PageTemplate(
            id="general_page_template",
            frames=[Frame(
                x1=1*cm,
                y1=3.5*cm,
                width=19*cm,
                height=23.2*cm,
                id="general_frame",
                showBoundary=1, # for testing purpose
            )])
        self.addPageTemplates(template)
        self.link_page = ""

    def afterFlowable(self, flowable):
        if isinstance(flowable, Paragraph):
            style_name = flowable.style.name
            self.link_page = str(self.page)
           
            if style_name == "crs_h1":
                # self.link_page = str(self.page)
                key = str(hash(flowable))
                self.canv.bookmarkPage(key)

            if style_name == "add_page":
                text = flowable.getPlainText()
                if self.link_page != "":
                    flowable.text = flowable.text.replace("link", self.link_page)
                    print(flowable.text)
```
Reply all
Reply to author
Forward
0 new messages