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

Added method to remove temporary files created #60

Closed TytoCapensis closed 1 year ago

TytoCapensis commented 2 years ago

Hello,

I added the method _remove_tmp_file which is supposed to remove the temporary files created when we supply a HTML string to screenshot() instead of providing a file. This prevents too much files from accumulating in the temporary directory.

Feel free to change the name of the method or to tell your opinions about this PR :)

vgalin commented 2 years ago

Hello, thanks for opening a PR, something like this is indeed missing from the package.

It could almost be merged as-is, but I think one should be able to choose whether or not they want the files to be automatically deleted. I can think of two reasons:

Here is how I would add the possibility to toggle the automatic deletion:


On a side note : as-is, this method only deletes HTML files and leaves CSS files untouched. It is not necessarily bad however : a CSS file can be loaded one time and be used by an infinite number of HTML files, so deleting them may sometimes be counter-productive.

Still, being able to delete everything in the tmp folder using one command would be nice, it could be a function that would have to be called manually.


Anyway, thanks again for your PR. I wrote everything that came to my mind to make sure that I won't forget about it.
If you wish to add anything that I mentioned in this comment, feel free to do so. If not, I'll add it later myself to this PR.

I'm currently focusing on making this package compatible with Chrome's and Firefox's CDP interface (which is a way to control a web browser via websockets), so any unrelated changes will come after that, sooner or later.

TytoCapensis commented 2 years ago

I added an improvement commit:

However, I am unsure to understand what you mean with CSS files: from my perspective, it looks like that only HTML files are created, since the CSS string we pass to the screenshot function is embedded into the HTML file. Are there particular circumstances in which temporary CSS files (and anything else than HTML) are supposed to be created?