Unable to create an image hyperlink with xhtml2pdf

时间:2015-09-29 00:43:31

标签: xhtml2pdf

Consider the following code:

from xhtml2pdf import pisa

if __name__=="__main__":
    source_HTML = """<html>
<body>
    <a href="http://www.stackoverflow.com">
        <img src="http://i.stack.imgur.com/uQFNA.png"/>
    </a>
    <a href="http://www.stackoverflow.com">
        <p>Stackoverflow</p>
    </a>
</body>
</html>"""

    output_filename = "output.pdf"

    # open output file for writing (truncated binary)
    result_file = open(output_filename, "w+b")

    # convert HTML to PDF
    pisa.CreatePDF(
        source_HTML,
        dest=result_file)

    result_file.close()

The html should render both an hyperlink image and a hyperlink text, but xhtml2pdf renders the image without the hyperlink and the text with the hyperlink. Is this an issue with the code above or a limitation/bug with the library? Thanks in advance for any help with the above issue.

1 个答案:

答案 0 :(得分:1)

我找到了一种方法来使这个工作得太好但是它不是很好。

您可以添加&amp; nbsp;在标签内部的img之前和之后。然后将A标记上的font-size设置为图像的高度,然后将text-decoration设置为none。

就像我说的那样有效,但不是一个很好的解决方案。我在xhtml2pdf的github页面上搜索了一些问题,找不到其他人。