Search code examples
pythonhtmlimagepdfxhtml2pdf

Does the xhtml2pdf python library support the <img> tag?


I am trying to export an html document to pdf using the xhtml2pdf python library.

I think the <img> tag is supported - however the docs are not clear on this matter - there are a couple of test cases using the tag.

Following the example in the docs, with an image added, I did this:

from xhtml2pdf import pisa
sourceHtml = "<html><body><div><img src ='testimage.jpg'></div><p>Some text output for testing...<p></body></html>"
outputFilename = "test.pdf"
resultFile = open(outputFilename, "w+b")
pisa.CreatePDF(sourceHtml,dest=resultFile)
resultFile.close()

However no image was included in the resulting pdf. Reading around, I see that this might be to do with the PIL package - which appears to be installed OK on my system.

My question is should I be expecting the above code to work with xhtml2pdf or does it ignore the <img> tag?


Solution

  • I'm trying to remember if this is the same issue, but if I remember my own experience, include the full path to the image.

    <img src ='c:\docs\testimage.jpg'>