Sign inSign up

mingalevme/screenshoter

By mingalevme

Updated 5 months ago

Google Puppeteer (screenshot) as a Dockerized HTTP-service

Image
3

9.4K

mingalevme/screenshoter repository overview

screenshoter

Google Puppeteer as a Dockerized HTTP-service for making screenshots.

nodesource/node

installation

docker pull mingalevme/screenshoter

Usage

Basic usage
docker run -d --restart always -p 8080:8080 --name screenshoter mingalevme/screenshoter

... or for development purposes:

docker build --rm -t local/screenshoter .
docker run --rm -p 8082:8080 --name screenshoter-local local/screenshoter
curl "http://localhost:8080/screenshot?url=https%3A%2F%2Fhub.docker.com%2Fr%2Fmingalevme%2Fscreenshoter%2F" > /tmp/screenshot.png
Specifying the cache dir
docker run -p 8080:8080 -v <cache_dir>:/var/cache/screenshoter mingalevme/screenshoter

First request:

time curl "http://localhost:8080/screenshot?url=https%3A%2F%2Fhub.docker.com%2Fr%2Fmingalevme%2Fscreenshoter%2F&ttl=3600" > /tmp/screenshot.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 43589    0 43589    0     0   4119      0 --:--:--  0:00:10 --:--:-- 10020

real	0m10.597s
user	0m0.007s
sys	0m0.005s

Second request:

time curl "http://localhost:8080/screenshot?url=https%3A%2F%2Fhub.docker.com%2Fr%2Fmingalevme%2Fscreenshoter%2F&ttl=3600" > /tmp/screenshot.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 43589    0 43589    0     0  5545k      0 --:--:-- --:--:-- --:--:-- 6081k

real	0m0.022s
user	0m0.006s
sys	0m0.004s

API Reference

GET /screenshot
Arguments
ArgTypeRequiredDescription
urlstringtrueAbsolute URL of the page to screenshot. Example: 'https://www.google.com'
formatstringfalseImage file format. Supported types are png or jpeg. Defaults to png.
qualityintfalseThe quality of the image, between 1-100. Not applicable to png images.
fullintfalseWhen true, takes a screenshot of the full scrollable page. Defaults to false.
devicestringfalseOne of supported device, e.g. iPhone X, see https://github.com/GoogleChrome/puppeteer/blob/master/DeviceDescriptors.js for a full list of devices
viewport-widthintfalseWidth in pixels of the viewport when taking the screenshot. Using lower values like 460 can help emulate what the page looks like on mobile devices. Defaults to 800.
viewport-heightintfalseHeight in pixels of the viewport when taking the screenshot. Defaults to 600.
is-mobilebool (int)falseWhether the meta viewport tag is taken into account. Defaults to false.
has-touchbool (int)falseSpecifies if viewport supports touch events. Defaults to false.
is-landscapebool (int)falseSpecifies if viewport is in landscape mode. Defaults to false.
device-scale-factorintfalseSets device scale factor (basically dpr) to emulate high-res/retina displays. Number from 1 to 4. Defaults to 1.
user-agentstringfalseSets user agent
cookiesjsonfalseList with cookies objects (https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies), e.g. [{"name":"foo","value":"bar","domain":".example.com"}]
timeoutintfalseMaximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout.
fail-on-timeoutbool (int)falseIf set to false, we will take a screenshot when timeout is reached instead of failing the request. Defaults to false.
delayintfalseIf set, we'll wait for the specified number of seconds after the page load event before taking a screenshot.
wait-until-eventstringfalseControls when the screenshot is taken as the page loads. Supported events include: load - window load event fired (default); domcontentloaded - DOMContentLoaded event fired; networkidle0 - wait until there are zero network connections for at least 500ms; networkidle2 - wait until there are no more than 2 network connections for at least 500ms. domcontentloaded is the fastest but riskiest option–many images and other asynchronous resources may not have loaded yet. networkidle0 is the safest but slowest option. load is a nice middle ground.Defaults to load.
elementstringfalseQuery selector of element to screenshot.
transparencybool (int)falseHides default white webpage background for capturing screenshots with transparency, only works when format is png. Defaults to 0.
widthintfalseIf resulted image's width is greater than provided value then image will be proportionally resized to provided width. This action runs before max-height checking. Defaults to 0 (do not resize).
max-heightintfalseIf resulted image's height is greater than provided value then image's height will be cropped to provided value. Defaults to 0 (do not crop).
ttlintfalseIf last cached screenshot was made less than provided seconds then the cached image will be returned otherwise image will be cached for future use.

Troubleshooting

BUS_ADRERR

If you got page crash with BUS_ADRERR (chromium issue), increase shm-size on docker run with --shm-size argument

docker run --shm-size 1G mingalevme/screenshoter
Navigation errors (unreachable url, ERR_NETWORK_CHANGED)

If you're seeing random navigation errors (unreachable url) it's likely due to ipv6 being enabled in docker. Navigation errors are caused by ERR_NETWORK_CHANGED (-21) in chromium. Disable ipv6 in your container using --sysctl net.ipv6.conf.all.disable_ipv6=1 to fix:

docker run --shm-size 1G --sysctl net.ipv6.conf.all.disable_ipv6=1 -v <cache_dir>:/var/cache/screenshoter mingalevme/screenshoter

Tag summary

Content type

Image

Digest

sha256:51d12b09b

Size

735.6 MB

Last updated

5 months ago

docker pull mingalevme/screenshoter