我正在尝试创建一个包含许多页面的 pdf 文档。每个页面都包含一个图像,其顶部显示带有指向其他页面的超链接的文本。
我已经设法使用reportlab.pdfgen
矩形链接来做到这一点,不幸的是,使用的阅读器(电子阅读器)无法识别这种类型的链接。
取而代之的是,我设法创建了文本嵌入的超链接(使用如何使用 reportlab 添加到单词的链接?),我的电子阅读器可以识别这些超链接,但我没有设法将它们覆盖在图像上。
我尝试使用这篇文章中提供的解决方案(reportlab: add background image by using platypus)来使用图像作为背景,但它不起作用。当我将文档的页面大小设置为图像大小时,不会显示该段落。当我将其设置为大于图像大小时,该段落显示在图像上方而不是覆盖它。
这是我的代码:
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import BaseDocTemplate, PageTemplate, Frame, Paragraph
def draw_static(canvas, doc):
# Save the current settings
canvas.saveState()
# Draw the image
canvas.drawImage(r'path\to\the\image.png', 0, 0, width=500, height=500)
# Restore setting to before function call
canvas.restoreState()
# Set up a basic template
doc = BaseDocTemplate('test.pdf', pagesize=(500, 500))
# Create a Frame for the Flowables (Paragraphs and such)
frame = Frame(doc.leftMargin, doc.bottomMargin, 500, 500, id='normal')
# Add the Frame to the template and tell the template to call draw_static for each page
template = PageTemplate(id='test', frames=[frame], onPage=draw_static)
# Add the template to the doc
doc.addPageTemplates([template])
# All the default stuff for generating a document
styles = getSampleStyleSheet()
story = []
link = '<link href="http://example.com">Text</link>'
P = Paragraph(link, styles["Normal"])
story.append(P)
doc.build(story)
PS:此代码不完整(不会创建多个页面等),但只是尝试在图像上覆盖文本嵌入超链接的最小示例。