Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full page screenshot? #32

Closed
youshy opened this issue May 19, 2020 · 8 comments
Closed

Full page screenshot? #32

youshy opened this issue May 19, 2020 · 8 comments
Assignees
Labels
enhance New feature or request

Comments

@youshy
Copy link
Contributor

youshy commented May 19, 2020

Is there a way in which we could get a screenshot of the whole page full size, not a screenshot of the window size?

@youshy youshy added the question Questions related to rod label May 19, 2020
@ysmood
Copy link
Member

ysmood commented May 19, 2020

How about to use page.PDF()?

@youshy
Copy link
Contributor Author

youshy commented May 19, 2020

I've ended up writing this:

func UseRod(link string) {
	url := launcher.New().
		Headless(false).
		Devtools(false).
		Launch()

	browser := rod.New().
		ControlURL(url).
		Trace(false).
		Connect().
		Timeout(time.Minute)

	defer browser.Close()
	page := browser.Timeout(time.Minute).Page(link)
	wait := page.WaitRequestIdle()
	wait()
	height := page.Eval(`() => document.body.clientHeight`).String()
	heightToInt, err := strconv.ParseInt(height, 10, 64)
	if err != nil {
		log.Fatalln(err)
	}
	page.Viewport(1920, heightToInt, 1, false)
	toByte, err := page.ScreenshotE(&proto.PageCaptureScreenshot{})

	f, err := os.Create("img_2.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	img, _, _ := image.Decode(bytes.NewReader(toByte))
	err = png.Encode(f, img)
	if err != nil {
		log.Fatal(err)
	}
}

but will try the page.PDF() as well! My solution is a tad verbose... 😆

@ysmood ysmood added enhance New feature or request and removed question Questions related to rod labels May 19, 2020
@ysmood
Copy link
Member

ysmood commented May 19, 2020

Let's keep this ticket open since the full-page screenshot is actually useful. I just don't have enough time to implement it. The common solution is to take multiple shots while scrolling down the page until it hits the bottom, then merge all those shots into one.

If you are interested in it, it'll be great if you can help to implement it.

@youshy
Copy link
Contributor Author

youshy commented May 19, 2020

Both page.PDF() and page.PDFE() returns

2020/05/19 15:37:54 {"code":-32000,"message":"PrintToPDF is not implemented","data":""}

@ysmood I'm more than happy with helping!

@ysmood
Copy link
Member

ysmood commented May 19, 2020

If your chrome version is too old, it doesn't support the PDF API.

The way you code it is similar to puppeteer, which changes the size of the frame. But puppeteer uses Emulation.setDeviceMetricsOverride to do it:

https://github.com/puppeteer/puppeteer/blob/caaf4d2086036d4bf201587529dd3e3b38b6f9c6/src/Page.ts#L1058-L1064

I think this kind of solution is good to have.

@ysmood
Copy link
Member

ysmood commented May 19, 2020

To make the chrome version consistent, you can download a specific version of chrome and specified the bin location:

launcher.New().Bin("the_path_of_chrome")

Or you can check this FAQ to use docker: https://github.com/ysmood/rod#q-how-to-use-rod-with-docker

@ysmood
Copy link
Member

ysmood commented May 21, 2020

Published v0.34.2 with the full page screenshot feature.

I changed the name to SceenshotFullPage, so that when user types Screenshot the IDE will give the user a list to choose which one to use.

Because of the retina scale bug, I have to revert the GetViewport, the safest way to do it is to store a private viewport whenever we set it, once chrome adds the API to retrieve the metrics we can optimize it.

@youshy
Copy link
Contributor Author

youshy commented May 21, 2020

Good call!

I'll close this one for now then. If you have any more tasks or functionalities you want to implement - hit me up, happy to help :) (I'll probably learn some stuff while using the lib tho, will try to add them here as I go)

@youshy youshy closed this as completed May 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhance New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants