Render HTML email as PNG
I have an email message in my IMAP inbox. It's HTML email that makes use of fonts, tables, images and such. How do I convert this email message into a PNG image that looks reasonably close to what I see in my email program?
I can almost accomplish what I want by printing to PDF in my email program and then using pdftoppm -png
to convert it to PNG. However, the PDF has page margins that the original email doesn't have. Also, pdftoppm
produces one PNG per page whereas I'd like to have the entire email in one PNG with no page breaks.
Taking a screenshot isn't an option because the email is very long.
Since HTML email is basically HTML, is there some method that involves getting the raw HTML out of the message and piping it to some kind of WebKit-based rendering tool?
Ended up doing this:
- Save the message to
message.eml
. - Convert
.eml
to.html
using this Python 3 script:
import email import sys msg = email.message_from_file(sys.stdin) for part in msg.walk(): if part.get_content_type() == "text/html": sys.stdout.buffer.write(part.get_payload(decode=True))
Run the script:
python3 eml-to-html.py < message.eml > message.html
Open the resulting
message.html
in Firefox.In the Firefox menu, choose Tools > Web Developer > Developer Toolbar to show a command line at the bottom of the browser window. Type
screenshot --fullpage message.png
there and press Enter (as per Journeyman Geek's suggestion).
If its a one-shot thing - firefox does this awesomely. Shift f2 opens up a console, and the command screenshot --fullpage filename
shows your screen exactly as you see it. Might need some work if its in a frame.
Not sure if its trivially automatable but it works quite well otherwise.