1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import ( "context" "math" "os" "github.com/chromedp/chromedp" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/page" )
func CaptureFullPageManual(ctx context.Context, quality int64) ([]byte, error) { _, _, contentMetrics, _, _, _, err := page.GetLayoutMetrics().Do(ctx) if err != nil { return nil, err } width := int64(math.Ceil(contentMetrics.Width)) height := int64(math.Ceil(contentMetrics.Height))
if err = emulation.SetDeviceMetricsOverride(width, height, 1, false). WithScreenOrientation(&emulation.ScreenOrientation{ Type: emulation.OrientationTypePortraitPrimary, Angle: 0, }).Do(ctx); err != nil { return nil, err }
buf, err := page.CaptureScreenshot(). WithQuality(quality). WithClip(&page.Viewport{ X: contentMetrics.X, Y: contentMetrics.Y, Width: contentMetrics.Width, Height: contentMetrics.Height, Scale: 1, }).Do(ctx) if err != nil { return nil, err } return buf, nil }
func main() { ctx, cancel := chromedp.NewContext(context.Background()) defer cancel()
if err := chromedp.Run(ctx, chromedp.Navigate("https://example.com")); err != nil { log.Fatal(err) }
buf, err := CaptureFullPageManual(ctx, 90) if err != nil { log.Fatal(err) } if err := os.WriteFile("manual_full.png", buf, 0o644); err != nil { log.Fatal(err) } }
|
Related Issues not found
Please contact @yiGmMk to initialize the comment