レポートのハイパーリンク画像を含むPDFをエクスポートしようとしています。レポート全体に画像があり、PDFに追加された画像へのハイパーリンクを追加して、画像を取得したフォルダにjpgを開きたいと思っていました。 @Meiloの素晴らしいソリューションを見つけたReportLab Image Linkと@missmely Is it possible to get a Flowable's coordinate position once it's rendered using ReportLab.platypus?Reportlab ImageクラスのHyperlinkedImageサブクラスが機能していません
しかし、私はHyperlinkClassからオブジェクトを作成しようとするとエラーが発生し続ける。誰かが私がオブジェクトを作成し、それを正しく参照する手助けをすることができますか?
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import Flowable, SimpleDocTemplate, Paragraph, Spacer
from reportlab.platypus import Image, PageBreak, KeepTogether, ListFlowable,
from reportlab.platypus import ListItem, Table, ListItem, Table
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
class HyperlinkedImage(Image, object):
"""
Class 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...Now instead of creating a reportlab.platypus.
Image as your image flowable, use the new HyperlinkedImage instead.
See original post:
https://stackoverflow.com/questions/18114820/is-it-possible-to-get-a-flowables-coordinate-position-once-its-rendered-using
"""
# 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)
# Create PDF
doc = SimpleDocTemplate(
"myHyperlinkedPics.pdf",
pagesize=letter,
rightMargin=60, leftMargin=60,
topMargin=60, bottomMargin=80)
Story = []
styleSheet = getSampleStyleSheet()
logo = "IamZeroInjury.png"
im = Image(logo, 1 * inch, 1 * inch)
Story.append(KeepTogether(im))
myHyperlinkedImage = HyperlinkedImage(logo, hyperlink=logo)
Story.append(KeepTogether(myhyperlinkedImage(hyperlink=logo, 1*inch, 1*inch,
1*inch, 1*inch)))
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
Story.append(PageBreak())
doc.build(Story)
私はこのコードを実行すると、私はエラーを取得:
in __getattr__
raise AttributeError("<Image @ 0x%x>.%s" % (id(self),a))
AttributeError: <Image @ 0x3752990>.hAlignAdjust