html2image icon indicating copy to clipboard operation
html2image copied to clipboard

is there a way to stop wrapping of html_str?

Open jtauber opened this issue 1 year ago • 3 comments

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 avatar Feb 11 '24 12:02 jtauber

My workaround is to just add the line:

Html2Image._prepare_html_string = lambda html, _: html

in my code.

jtauber avatar Feb 11 '24 12:02 jtauber

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.

vgalin avatar Feb 11 '24 13:02 vgalin

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

jtauber avatar Feb 11 '24 23:02 jtauber