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

is there a way to stop wrapping of html_str? #140

Open jtauber opened 8 months ago

jtauber commented 8 months ago

I noticed after a while using this library (when I set keep_temp_files=True) that my html_str is being wrapped as if it were just the body. I can see this is being done _prepare_html_string where the parameter is called html_body (which makes it clearer that it's just the body).

Is there any way to stop this wrapping and just pass in the full HTML?

Perhaps with a different argument?

jtauber commented 8 months ago

My workaround is to just add the line:

Html2Image._prepare_html_string = lambda html, _: html

in my code.

vgalin commented 8 months ago

At the time, I did not consider that html_str would be used with something other than a very basic HTML string; it was more of a convenient way to transform a string into a valid HTML file than anything. But I indeed see cases where you could already have a full HTML file stored as a string and wouldn't want to transform it in any way.

Currently, there is no "right" way to disable this behavior; your workaround is probably the shortest/cleanest way to disable _prepare_html_string.

Another way could be to write your HTML strings to files directly and call the screenshot method using the html_file parameter, as the HTML files are not pre-processed in any way.

In any case, a simple boolean parameter allowing to disable this behavior would be nice; it will be added when I'll have access to my machine.

jtauber commented 8 months ago

Thank you! Hopefully it's helpful to others too!