使用 ReportLab.platypus 渲染后是否可以获得 Flowable 的坐标位置? [英] Is it possible to get a Flowable's coordinate position once it's rendered using ReportLab.platypus?

查看:73
本文介绍了使用 ReportLab.platypus 渲染后是否可以获得 Flowable 的坐标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要目标是让页面上的所有 Image flowables 都表现得好像它们是可点击的链接.为了做到这一点,我将创建一个 canvas.linkRect() 并将其放置在渲染的图像上.这是我如何使用 canvas.linkRect() 的示例:

My main goal is to have all the Image flowables on a page to act as though they are clickable links. In order to do this, I would create a canvas.linkRect() and place it over the rendered Image. Here's an example of how I use canvas.linkRect():

canvas.linkURL(
    url='url_goes_here',
    rect=(x1, y1, x2, y2), #(x1, y1) is the bottom left coordinate of the rectangle, (x2, y2) is the top right
    thickness=0, relative=1
)

在 BaseDocTemplate 类中查找后,我发现了一个名为 afterFlowable(self, flowable) 的方法.我覆盖了该方法并在传入的 flowable 上调用了 dir() ,结果是:

After looking in the BaseDocTemplate class, I found a method called afterFlowable(self, flowable). I overrode that method and called dir() on the flowable passed in, resulting in this:

['__call__', '__doc__', '__init__', '__module__', '_doctemplateAttr',
'_drawOn', '_fixedHeight', '_fixedWidth', '_frameName', '_hAlignAdjust',
'_showBoundary', '_traceInfo', 'action', 'apply', 'draw', 'drawOn', 'encoding',
'getKeepWithNext', 'getSpaceAfter', 'getSpaceBefore', 'hAlign', 'height',
'identity', 'isIndexing', 'locChanger', 'minWidth', 'split', 'splitOn', 'vAlign',
'width', 'wrap', 'wrapOn', 'wrapped']

它有一个 width 和 height 属性,我可以用它来确定 linkRect() 应该有多大(x2 和 y2 应该是什么),但没有关于可流动的开始位置的信息(x1 和 y1 应该是什么?).

It has a width and height attribute I could use to determine how big the linkRect() should be (what x2 and y2 should be), but no information on where the flowable starts (what should x1 and y1 be?).

如果所有其他方法都失败了,我想以某种方式将 Frame 和 Image Flowable 配对在一起,因为 Frame 具有我想要创建 linkRect() 的信息.但是,除了必须确切知道将这些图像帧放在哪里之外,似乎还很难知道何时以及如何使用其各自的 Flowable 列表对帧列表进行排序.有没有其他方法可以实现这一点,或者是不可能的?

If all else fails, I thought of somehow pairing a Frame and an Image Flowable together since a Frame has the information I want to create a linkRect(). However, it seems it would be a hassle to know when and how to order a list of Frames with it's respective list of Flowables, on top of having to know exactly where to put those Frames for Images. Is there another way to achieve this or is it not possible?

谢谢!

推荐答案

在今天工作了一整天之后,我终于找到了一个很好的方法来做到这一点!以下是我为其他想要在其 PDF 中使用超链接图像流动功能的人所做的工作.

After working on this all day today, I finally figured out a nice way to do this! Here's how I did it for anyone else who would like this feature of hyperlinked Image flowables in their PDFs.

基本上,reportlab.platypus.flowables 有一个名为 Flowable 的类,Image 继承自该类.Flowable 有一个名为 drawOn(self, canvas, x, y, _sW=0) 的方法,我在我创建的一个名为 HyperlinkedImage 的新类中重写了该方法.

Basically, reportlab.platypus.flowables has a class called Flowable that Image inherits from. Flowable has a method called drawOn(self, canvas, x, y, _sW=0) that I override in a new class I created called HyperlinkedImage.

from reportlab.platypus import Image

class HyperlinkedImage(Image, object):

    # The only variable I added to __init__() is hyperlink. I default it to None for the if statement I use later.
    def __init__(self, filename, hyperlink=None, width=None, height=None, kind='direct', mask='auto', lazy=1):
        super(HyperlinkedImage, self).__init__(filename, width, height, kind, mask, lazy)
        self.hyperlink = hyperlink

    def drawOn(self, canvas, x, y, _sW=0):
        if self.hyperlink: # If a hyperlink is given, create a canvas.linkURL()
            x1 = self.hAlignAdjust(x, _sW) # This is basically adjusting the x coordinate according to the alignment given to the flowable (RIGHT, LEFT, CENTER)
            y1 = y
            x2 = x1 + self._width
            y2 = y1 + self._height
            canvas.linkURL(url=self.hyperlink, rect=(x1, y1, x2, y2), thickness=0, relative=1)
        super(HyperlinkedImage, self).drawOn(canvas, x, y, _sW)

现在不是创建 reportlab.platypus.Image 作为您的图像流动,而是使用新的 HyperlinkedImage :)

Now instead of creating a reportlab.platypus.Image as your image flowable, use the new HyperlinkedImage instead :)

这篇关于使用 ReportLab.platypus 渲染后是否可以获得 Flowable 的坐标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆