imgkit icon indicating copy to clipboard operation
imgkit copied to clipboard

How to load from a local image file?

Open XieGuochao opened this issue 3 years ago • 1 comments

I have included an in my HTML whose src is a local image file. However, the package tries to download it from the network. I believe this bug deserves works to fix.

OSError: wkhtmltoimage reported an error: Loading page (1/2) Warning: Blocked access to file /home/david/imageMaster/bg.jfif
Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown

XieGuochao avatar Jun 18 '21 03:06 XieGuochao

Was also suffering from this problem and this is the way I found to solve it.

1º Insure that in the options you include both this keys:

enable-local-file-access is to allow wkhtmltoimage access to local files allow should be the base absolute path where you have the image, in my case a folder named 'templates' inside my project.

options = {
            ...,
            'enable-local-file-access': "",
            'allow': os.path.join(os.path.dirname(__file__), 'templates') 
        }

2º After that I pass the full path for the image to my html template with resource to Jinja 2

<img class="fancy-img my-logo"  src = "{{ logo }}" alt="My Logo"/>

3º I pass the rendered html from Jinja to imgkit and my problem is solved.

Hope that helps

ltfavas avatar Jan 06 '22 11:01 ltfavas