Take a Screenshot of a Webpage with Headless Chrome
·
275 words
·
2 minutes read
In this post we will look at leveraging Chrome’s debug protocol to load up a webpage and take a screenshot. This is all made possible from a package called chromedp
which allows us to control a Chrome instance through our Go code. You will also need Chrome installed or to be using something akin to the chrome/headless-shell
docker image.
We’ve split the process in code up into:
- Start Chrome
- Run tasks: like loading the webpage and taking a screenshot
- Saving the screenshot to file
|
|
As an added bonus, if you wanted to save the page as a pdf instead of an image, you can replace the CaptureScreenshot
line with following:
|
|