使用chromedp从HTML字符串渲染PNG对象

问题描述

我对Chromedp非常陌生。我知道如何从Chromdp的网址呈现图像,但是有没有办法使用Chromedp从HTML字符串本身呈现图像?我可以使用以下方法将网址转换为图片

func fullScreenshot(urlstr string,quality int64,res *[]byte) chromedp.Tasks {
    return chromedp.Tasks{
        chromedp.Navigate(urlstr),chromedp.Sleep(3 * time.Second),chromedp.ActionFunc(func(ctx context.Context) error {
            // get layout metrics
            _,_,contentSize,err := page.GetLayoutMetrics().Do(ctx)
            if err != nil {
                return err
            }

            width,height := int64(math.Ceil(contentSize.Width)),int64(math.Ceil(contentSize.Height))

            // force viewport emulation
            err = emulation.SetDeviceMetricsOverride(width,height,1,false).
                WithScreenorientation(&emulation.Screenorientation{
                    Type:  emulation.OrientationTypePortraitPrimary,Angle: 0,}).
                Do(ctx)
            if err != nil {
                return err
            }

            // capture screenshot
            *res,err = page.CaptureScreenshot().
                WithQuality(quality).
                WithClip(&page.Viewport{
                    X:      contentSize.X,Y:      contentSize.Y,Width:  contentSize.Width,Height: contentSize.Height,Scale:  1,}).Do(ctx)
            if err != nil {
                return err
            }
            return nil
        }),}
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)