raja.pallavan has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I need any module to convert html file to image file(jpeg) with custom resolution.

Regards,

Raja E.,

Chennai.

Replies are listed 'Best First'.
Re: html to jpeg
by marto (Cardinal) on Jul 16, 2012 at 11:10 UTC

    I assume you want a screen shot of the HTML rendered, rather than the code ;) WWW::Mechanize::Firefox has a screenshot example for opening and rendering a page and saving (a screenshot of) it as a png (rather than jpeg) file.

    Update: slight reword of the last sentence.

Re: html to jpeg
by daxim (Curate) on Jul 16, 2012 at 11:11 UTC

      A slight correction - WWW::Mechanize::Firefox doesn't do a screenshot per se, it renders the page to a <canvas> HTML object and then transfers the canvas data back to Perl. But the dimensions of the resulting image are mostly determined by the size of the browser window still, so it is quite close to a screenshot still.

Re: html to jpeg
by zentara (Cardinal) on Jul 16, 2012 at 11:35 UTC
    If you are on linux, you can use this script to take a snapshot of the browser. There is one prerequisite in that you must be on the same virtual desktop as the web browser when you run this script. You also get the whole browser, not just the html. The second script below just grabs the html using Gtk2::Webkit

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: html to jpeg
by cavac (Prior) on Jul 16, 2012 at 13:04 UTC

    Depending on your requirements, you could also "print" the page to PDF, for example automating the process with X11::GUITest (or Win32::GUITest on windows). Here's an example: 945042 on automating browser interaction.

    If you choose to do it via screenshots, as long as the browser window stays the same size, it's trivial to remove the browser borders from the images via something like Image::Magick

    "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"
Re: html to jpeg
by petdance (Parson) on Jul 16, 2012 at 20:16 UTC
    If you're taking screenshots, please do NOT use JPEG. JPEG is only for photographs, and a screenshot is not a photograph. The compression algorithm in JPEG will introduce ugly screen artifacts.

    If you want a clean, sharp screenshot, save it in PNG or TIFF format.

    xoxo,
    Andy