Search code examples
pythondjangopdfkit

pdfkit image not embedded using Django


I'm converting a protected webpage into pdf using Django and this solution.

The solution works great except the part that the webpage contains images which do not show up in the pdf and i get an error

Warning: Failed to load file:///media/images/main/logo.jpg (ignore)

I'm currently using from_string method as follows

pdfkit.from_string(msg_html, 'testing123.pdf') #msg_html contains html string with images

Solution

  • turns out that url of images in html document that needs to be converted to pdf using pdfkit needs to have an absolute url

    in my html file, i had to update my urls as follows:

    <img src="/media/images/main/logo.jpg">
    

    to

    <img src="https://test.mywebsite.com/media/images/main/logo.jpg">
    

    using this i was able to generated pdf containing my logo from the html file