From c4f094ca2cc0bf3c7e1d3d3684cdd038173d67c6 Mon Sep 17 00:00:00 2001 From: gaowei Date: Thu, 14 Jul 2022 18:13:11 +0800 Subject: [PATCH] feat: support set OS params convert to image --- controller/html2image_controller.go | 4 ++++ controller/types.go | 2 ++ converter/doctron_core/html2image.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/controller/html2image_controller.go b/controller/html2image_controller.go index b07d853..03e1bc7 100644 --- a/controller/html2image_controller.go +++ b/controller/html2image_controller.go @@ -102,6 +102,8 @@ func convertToHtml2ImageParams(requestDTO *Html2ImageRequestDTO) doctron_core.Ht params.Clip.Scale = requestDTO.ClipScale params.FromSurface = requestDTO.FromSurface params.WaitingTime = requestDTO.WaitingTime + params.IsAndroid = requestDTO.IsAndroid + params.IsIPhone = requestDTO.IsIPhone return params } @@ -117,5 +119,7 @@ func newDefaultHtml2ImageRequestDTO() *Html2ImageRequestDTO { ClipScale: doctron_core.DefaultViewportScale, FromSurface: doctron_core.DefaultFromSurface, WaitingTime: doctron_core.DefaultWaitingTime, + IsAndroid: doctron_core.DefaultIsAndroid, + IsIPhone: doctron_core.DefaultIsIPhone, } } diff --git a/controller/types.go b/controller/types.go index d30a6fc..24a9961 100644 --- a/controller/types.go +++ b/controller/types.go @@ -41,6 +41,8 @@ type Html2ImageRequestDTO struct { ClipScale float64 `schema:"clipScale,omitempty" validate:"omitempty"` // Capture the screenshot of a given region only.Page scale factor. FromSurface bool `schema:"fromSurface,omitempty" validate:"omitempty"` // Capture the screenshot from the surface, rather than the view. Defaults to true. WaitingTime int `schema:"waitingTime,omitempty" validate:"omitempty"` // Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond + IsAndroid bool `schema:"isAndroid,omitempty" validate:"omitempty"` // IsAndroid is an action to emulate a android device + IsIPhone bool `schema:"isIPhone,omitempty" validate:"omitempty"` // IsIPhone is an action to emulate a ios device } type PdfWatermarkRequestDTO struct { diff --git a/converter/doctron_core/html2image.go b/converter/doctron_core/html2image.go index 889601f..48e2fd6 100644 --- a/converter/doctron_core/html2image.go +++ b/converter/doctron_core/html2image.go @@ -8,6 +8,7 @@ import ( "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/page" "github.com/chromedp/chromedp" + "github.com/chromedp/chromedp/device" ) //FormatPng Image compression format (defaults to png). @@ -38,11 +39,23 @@ const DefaultViewportHeight = 996 //DefaultViewportScale Page scale factor. const DefaultViewportScale = 1 +//DefaultIsAndroid is an action to emulate a android device. Defaults to false. +const DefaultIsAndroid = false + +//DefaultIsIPhone is an action to emulate a ios device. Defaults to false. +const DefaultIsIPhone = false + //Html2ImageParams print page as PDF. type Html2ImageParams struct { page.CaptureScreenshotParams CustomClip bool WaitingTime int // Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond + OS +} + +type OS struct { + IsAndroid bool + IsIPhone bool } //NewDefaultHtml2ImageParams default html convert to image params @@ -62,6 +75,10 @@ func NewDefaultHtml2ImageParams() Html2ImageParams { FromSurface: DefaultFromSurface, }, WaitingTime: DefaultWaitingTime, + OS: OS{ + IsAndroid: DefaultIsAndroid, + IsIPhone: DefaultIsIPhone, + }, } } @@ -73,6 +90,16 @@ func (ins *html2image) GetConvertElapsed() time.Duration { return ins.convertElapsed } +func setDevices(o OS) chromedp.EmulateAction { + if o.IsAndroid { + return chromedp.Emulate(device.GalaxyNote3) + } else if o.IsIPhone { + return chromedp.Emulate(device.IPhoneX) + } else { + return chromedp.EmulateReset() + } +} + func (ins *html2image) Convert() ([]byte, error) { start := time.Now() defer func() { @@ -87,6 +114,7 @@ func (ins *html2image) Convert() ([]byte, error) { defer cancel() if err := chromedp.Run(ctx, + setDevices(params.OS), chromedp.Navigate(ins.cc.Url), chromedp.Sleep(time.Duration(params.WaitingTime)*time.Millisecond), chromedp.ActionFunc(func(ctx context.Context) error {