vgalin / html2image

A package acting as a wrapper around the headless mode of existing web browsers to generate images from URLs and from HTML+CSS strings or files.
MIT License
354 stars 43 forks source link

linked css vs css_file parameter #139

Closed jtauber closed 8 months ago

jtauber commented 8 months ago

I'm running html2image with local html+css.

I'm confused by the interaction of having a <link ref="stylesheet" ...> in the html vs specifying it via css_file. I seem to need both to get things to work.

It also appears it won't work if these are paths. I have to have my html and css in the same directory for any of it work.

Or am I just missing something?

vgalin commented 8 months ago

This may indeed need to be clarified somewhere, this behavior is not really well documented. Similarly to #47 , using absolute paths for files within <link ref="stylesheet" ...> should do the trick. This is because HTML2Image uses a temporary directory to allow the use of both HTML/CSS files and strings: files are copied to this directory and strings are converted+written as files in this directory.

Alternatively, if you only use existing files and not strings, you could change the temporary directory to the root directory of your html_file, like so:

from html2image import Html2Image
hti = Html2Image(temp_path='./') # assuming the root of your project is the current directory
...
jtauber commented 8 months ago

Thank you for such a quick and helpful response!