Make a web page screenshot service

I'll take you step by step into how to make a service that takes screenshots of webpages and returns them as an image.

First, let's assume you have google-chrome or chromium-browser installed. Both should work the same way. These browsers have command line options that let you capture a screenshot in headless mode.

chromium-browser --headless 
    --disable-gpu 
    --no-sandbox 
    --screenshot=out.png 
    --window-size=1280,900 
    --virtual-time-budget=1000 
    --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" 
    https://cbc.ca/news

Explanation of parameters

Url of the site to screenshot
--headlessPut it in headless mode
--disable-gpuUsing GPU in the background sometimes gives us problems, so we disable it
--no-sandboxThe sandbox often gives problems with headless execution as well.
--screenshot="<filename>"You can tell it to capture the screenshot to this file.
--window-size=width,heightSet the width and height of the window
--virtual-time-budget=<ms>Wait this many milliseconds before taking the screenshot, to give the site time to execute frontend frameworks.
--useragent="<user agent>"Set a custom user agent, since many sites will not work with the default one used in headless mode.
url

Making a service

Let's make it into a docker image that we can use. I won't go into the details, since I merely asked AI to do it for me. Synchronize the repo it made here:

git clone github.com/smhanov/screenshot-service
cd screenshot-service
make build
make run 

This will build and create your screenshotting service.Then you can get the screenshot of any web site by going to the url:

http://localhost:5000/screenshot?url=https://cbc.ca/news

It also accepts other parameters for the various arguments.

Comments