Skip to content

feat: support set OS params convert to image #32

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions controller/html2image_controller.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
2 changes: 2 additions & 0 deletions controller/types.go
Original file line number Diff line number Diff line change
@@ -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 {
28 changes: 28 additions & 0 deletions converter/doctron_core/html2image.go
Original file line number Diff line number Diff line change
@@ -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 {