Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdfkit not converting image to pdf

Tags:

python

pdfkit

I am using PDFkit with python to convert an html page to pdf. In the html there is only one image tag in body with src pointing to a complete url like:

<html>
    <body style="margin: 0px;">
        <img style="-webkit-user-select: none; cursor: -webkit-zoom-in;" src="https://blah.blah.com" height="768">
    </body>
</html>

However When I convert html to pdf like this:

pdfkit.from_file(file, 'labels.pdf', configuration=config)

I get a blank page with a box with border instead of image.

Why is pdfkit not converting image?

I read somewhere that we have to give full image path including domain name. But the image url I am providing is complete. Then what I am doing wrong?

like image 715
Manish Gupta Avatar asked Jul 12 '16 13:07

Manish Gupta


1 Answers

I encoded the image as a string of Base64 data, following @Manish Gupta's suggestion. In Python:

import base64

def get_image_file_as_base64_data():
    with open(FILEPATH, 'rb') as image_file:
        return base64.b64encode(image_file.read())

In my Jinja2 template (I know the question isn't specific to Jinja2, but this is what I'm using):

<img src="data:;base64,{{ get_image_file_as_base64_data() }}">
like image 132
Josh Avatar answered Sep 18 '22 10:09

Josh