2013-08-07 2 views
5

나의 주요 목표는 페이지상의 모든 이미지 플로우를 클릭 가능한 링크처럼 작동시키는 것입니다. 이를 위해 canvas.linkRect()를 만들어 렌더링 된 이미지 위에 놓습니다. 여기에 내가 canvas.linkRect를 (사용 방법의 예)입니다 :ReportLab.platypus를 사용하여 Flowable의 좌표 위치를 렌더링 할 수 있습니까?

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 (자체, 유동성)라는 방법을 발견했다.

['__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'] 

그것은 폭과 높이가 나는 linkRect()가 (어떤 X2 어떻게해야 큰 결정하는 데 사용할 수있는 속성과 :이 결과, 전달 된 유동성에 그 방법이라는 디렉토리()를 오버라이드 y2가 있어야 함). 그러나 유동성이 시작되는 위치에 대한 정보는 없습니다 (x1과 y1은 무엇이되어야합니까?).

그 밖의 모든 것이 실패하면 Frame에 linkRect()를 만들려는 정보가 있으므로 어떻게 든 Frame과 Image Flowable을 쌍으로 연결한다고 생각했습니다. 그러나 이미지의 프레임을 정확히 어디에 넣어야 할지를 아는 것 외에도 Flow List의 각 목록이있는 프레임 목록을 언제 어떻게 어떻게 주문해야하는지 알리는 것이 번거 롭습니다. 이것을 달성하는 또 다른 방법이 있습니까 아니면 불가능합니까?

감사합니다.

답변

8

오늘 하루 종일이 작업을 마친 후 마침내이 작업을 수행 할 수있는 좋은 방법을 찾았습니다! PDF에서 하이퍼 링크 된 이미지 플로우 룰의이 기능을 원하는 다른 모든 사람들을 위해 내가 한 일은 다음과 같습니다.

기본적으로 reportlab.platypus.flowables에는 이 상속하는 Flowable이라는 클래스가 있습니다. Flowable은 내가 HyperlinkedImage이라는 새 클래스에서 오버라이드하는 drawOn(self, canvas, x, y, _sW=0)이라는 메소드를 가지고 있습니다. 대신 이미지 유동성으로 reportlab.platypus.Image을 만드는

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) 

지금, 대신 새로운 HyperlinkedImage를 사용 :