html2image icon indicating copy to clipboard operation
html2image copied to clipboard

`html_str` parameter not working

Open kbuilds opened this issue 2 years ago • 2 comments

I am using Django to render an HTML file, then feed that file into html2image.

I am using the latest release: html2image==2.0.1

On Linux, using Chromium 93.0.4577.82 snap (Ubuntu 20.04)

To replicate:

hti = Html2Image()
hti.screenshot(html_str="<h1>Hey there</h1>")

This outputs a screenshot.png in my current working directory, however this screenshot is just a transparent image file, with no html content in it.

I tracked down the source of this issue to be the fact that on new versions of Ubuntu, Chromium is delivered only as a snap package, even if you use apt to install it (in this case, apt just uses snap under the hood). The problem with snap-installed packages is that they don't have access to the filesystem outside of the user's home directory. This includes the /tmp folder.

This took me a while to hunt down, and I feel like other less-python-inclined developers may also run into this issue, especially if they don't know a lot about Ubuntu. My fix was to pass the temp_path parameter, which I was only able to find by going through your source.

This parameter is only alluded to here in your CLI docs:

https://github.com/vgalin/html2image#using-the-cli

Once I found this, it was a simple fix:

hti = Html2Image(temp_path="/home/myuser")
hti.screenshot(html_str="<h1>Hey there</h1>")

And it worked. My humble recommendation would be to add to the docs to explain this parameter, and maybe a blurb about the limitations of Chromium installed via snap.

Thanks for your work on this project. It's really useful.

kbuilds avatar Sep 28 '21 22:09 kbuilds

Thank you for opening this very detailed issue. I took the (bad) habit to only answer issues after fixing them, but as you posted it more than a month ago I wanted to inform you that it didn't went unnoticed.

Regarding the temp_path parameter, it is mentionned in the usage section of the readme, but you have to click on the text reading "Multiple arguments can be passed to the constructor (click to expand)" to show the section mentionning it. It may be better to make this part visible by default ...

TODO:

  • Add a warning when a snap-based version of Chromium is used.
  • Use a different tmp directory when a snap-based version of Chromium is used.
  • Make changes to the readme regarding the constructor parameters'.

vgalin avatar Nov 09 '21 10:11 vgalin

Hi @vgalin, I got blank files when used the lib, and it looks like a folder permissions problem.

I think use tempfile.TemporaryDirectory instead of use a static /tmp/html2image folder, can solve this problem.

May I open a pull request doing this?

jjpaulo2 avatar Jul 05 '23 23:07 jjpaulo2