Skip to content

Commit 2f2e1f9

Browse files
author
Nick
committed
support wating time for html convert to pdf or image
1 parent e5e5334 commit 2f2e1f9

File tree

10 files changed

+64
-34
lines changed

10 files changed

+64
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### v0.3.0
2+
- support waiting time for html convert to pdf
3+
- support waiting time for html convert to image
14
### v0.2.0
25
- add pprof
36
- add chinese readme doc

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.DEFAULT: help
22

33
IMAGE_NAME ?= lampnick/doctron
4-
CENTOS_IMAGE_TAG ?= v0.2.0-centos
5-
ALPINE_IMAGE_TAG ?= v0.2.0-alpine
4+
CENTOS_IMAGE_TAG ?= v0.3.0-centos
5+
ALPINE_IMAGE_TAG ?= v0.3.0-alpine
66

77
help: Makefile
88
@echo "Doctron is a document convert tools for html pdf image etc.\r\n"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ http://127.0.0.1:8080/convert/html2pdf?u=doctron&p=lampnick&url=<url>&marginTop=
9999
- marginRight // Right margin in inches. core.Defaults to 1cm (~0.4 inches).
100100
- pageRanges // Paper ranges to print, e.g., '1-5, 8, 11-13'. core.Defaults to the empty string, which means print all pages.
101101
- ignoreInvalidPageRanges // Whether to silently ignore invalid but successfully parsed page ranges, such as '3-2'. core.Defaults to false.
102+
- WaitingTime // Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond
102103

103104
### Html convert to image
104105
###### basic
@@ -121,6 +122,7 @@ http://127.0.0.1:8080/convert/html2image?u=doctron&p=lampnick&url=<url>&customCl
121122
- clipY // Capture the screenshot of a given region only.Y offset in device independent pixels (dip).
122123
- clipWidth // Capture the screenshot of a given region only.Rectangle width in device independent pixels (dip).
123124
- clipHeight // Capture the screenshot of a given region only.Rectangle height in device independent pixels (dip).
125+
WaitingTime // Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond
124126

125127
### Pdf add watermark
126128
###### add image watermark

README_ZH.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ http://127.0.0.1:8080/convert/html2pdf?u=doctron&p=lampnick&url=<url>&marginTop=
100100
- marginRight // 右外边距,单位英尺。默认纸0.4英尺(1厘米).
101101
- pageRanges // 需要打印的PDF的页数。默认为空字符串,表示所有页面.
102102
- ignoreInvalidPageRanges // 是否静默的忽略掉不可用的但是成功解析的页面。例如'3-2',默认false.
103+
- WaitingTime // 页面加载后等待时长. 默认为0代表不等待. 单位:毫秒
103104

104105
### Html转图片
105106
###### 基础转换
@@ -122,6 +123,7 @@ http://127.0.0.1:8080/convert/html2image?u=doctron&p=lampnick&url=<url>&customCl
122123
- clipY // 裁剪Y轴方向距离.
123124
- clipWidth // 裁剪宽度.
124125
- clipHeight // 裁剪高度.
126+
- WaitingTime // 页面加载后等待时长. 默认为0代表不等待. 单位:毫秒
125127

126128
### Pdf加水印
127129
###### 添加图片水印

common/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package common
22

33
//Version Version
4-
const Version = "0.2.0"
4+
const Version = "0.3.0"

controller/html2image_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package controller
33
import (
44
"context"
55
"errors"
6+
"time"
7+
68
"github.com/gorilla/schema"
79
uuid "github.com/iris-contrib/go.uuid"
810
"github.com/kataras/iris/v12"
@@ -13,7 +15,6 @@ import (
1315
"github.com/lampnick/doctron/converter/doctron_core"
1416
"github.com/lampnick/doctron/worker"
1517
"gopkg.in/go-playground/validator.v9"
16-
"time"
1718
)
1819

1920
func Html2ImageHandler(ctx iris.Context) {
@@ -100,6 +101,7 @@ func convertToHtml2ImageParams(requestDTO *Html2ImageRequestDTO) doctron_core.Ht
100101
params.Clip.Height = requestDTO.ClipHeight
101102
params.Clip.Scale = requestDTO.ClipScale
102103
params.FromSurface = requestDTO.FromSurface
104+
params.WaitingTime = requestDTO.WaitingTime
103105
return params
104106
}
105107

@@ -114,5 +116,6 @@ func newDefaultHtml2ImageRequestDTO() *Html2ImageRequestDTO {
114116
ClipHeight: doctron_core.DefaultViewportHeight,
115117
ClipScale: doctron_core.DefaultViewportScale,
116118
FromSurface: doctron_core.DefaultFromSurface,
119+
WaitingTime: doctron_core.DefaultWaitingTime,
117120
}
118121
}

controller/html2pdf_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package controller
33
import (
44
"context"
55
"errors"
6+
"time"
7+
68
"github.com/gorilla/schema"
79
uuid "github.com/iris-contrib/go.uuid"
810
"github.com/kataras/iris/v12"
@@ -13,7 +15,6 @@ import (
1315
"github.com/lampnick/doctron/converter/doctron_core"
1416
"github.com/lampnick/doctron/worker"
1517
"gopkg.in/go-playground/validator.v9"
16-
"time"
1718
)
1819

1920
func Html2PdfHandler(ctx iris.Context) {
@@ -106,6 +107,7 @@ func convertToPDFParams(requestDTO *Html2PdfRequestDTO) doctron_core.PDFParams {
106107
params.HeaderTemplate = requestDTO.HeaderTemplate
107108
params.FooterTemplate = requestDTO.FooterTemplate
108109
params.PreferCSSPageSize = requestDTO.PreferCSSPageSize
110+
params.WaitingTime = requestDTO.WaitingTime
109111
return params
110112
}
111113

@@ -126,5 +128,6 @@ func newDefaultHtml2PdfRequestDTO() *Html2PdfRequestDTO {
126128
HeaderTemplate: doctron_core.DefaultHeaderTemplate,
127129
FooterTemplate: doctron_core.DefaultFooterTemplate,
128130
PreferCSSPageSize: doctron_core.DefaultPreferCSSPageSize,
131+
WaitingTime: doctron_core.DefaultWaitingTime,
129132
}
130133
}

controller/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Html2PdfRequestDTO struct {
2626
HeaderTemplate string `schema:"headerTemplate,omitempty" validate:"omitempty"` // HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: - date: formatted print date - title: document title - url: document location - pageNumber: current page number - totalPages: total pages in the document For example, <span class=title></span> would generate span containing the title.
2727
FooterTemplate string `schema:"footerTemplate,omitempty" validate:"omitempty"` // HTML template for the print footer. Should use the same format as the headerTemplate.
2828
PreferCSSPageSize bool `schema:"preferCSSPageSize,omitempty" validate:"omitempty"` // Whether or not to prefer page size as defined by css. core.Defaults to false, in which case the content will be scaled to fit the paper size.
29+
WaitingTime int `schema:"waitingTime,omitempty" validate:"omitempty"` // Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond
2930
}
3031

3132
type Html2ImageRequestDTO struct {
@@ -39,6 +40,7 @@ type Html2ImageRequestDTO struct {
3940
ClipHeight float64 `schema:"clipHeight,omitempty" validate:"omitempty"` // Capture the screenshot of a given region only.Rectangle height in device independent pixels (dip).
4041
ClipScale float64 `schema:"clipScale,omitempty" validate:"omitempty"` // Capture the screenshot of a given region only.Page scale factor.
4142
FromSurface bool `schema:"fromSurface,omitempty" validate:"omitempty"` // Capture the screenshot from the surface, rather than the view. Defaults to true.
43+
WaitingTime int `schema:"waitingTime,omitempty" validate:"omitempty"` // Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond
4244
}
4345

4446
type PdfWatermarkRequestDTO struct {

converter/doctron_core/html2image.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,49 @@ package doctron_core
33
import (
44
"context"
55
"errors"
6+
"time"
7+
68
"github.com/chromedp/cdproto/emulation"
79
"github.com/chromedp/cdproto/page"
810
"github.com/chromedp/chromedp"
9-
"time"
1011
)
1112

12-
// Image compression format (defaults to png).
13+
//FormatPng Image compression format (defaults to png).
1314
const FormatPng = page.CaptureScreenshotFormatPng
15+
16+
//FormatJpeg Image compression format (defaults to png).
1417
const FormatJpeg = page.CaptureScreenshotFormatJpeg
1518

16-
// Compression quality from range [0..100] (jpeg only).
19+
//DefaultQuality Compression quality from range [0..100] (jpeg only).
1720
const DefaultQuality = 0
1821

19-
// Capture the screenshot from the surface, rather than the view. Defaults to true.
22+
//DefaultFromSurface Capture the screenshot from the surface, rather than the view. Defaults to true.
2023
const DefaultFromSurface = true
2124

22-
// Capture the screenshot of a given region only.
25+
//DefaultViewportX Capture the screenshot of a given region only.
2326
// X offset in device independent pixels (dip).
2427
const DefaultViewportX = 0
2528

26-
// Y offset in device independent pixels (dip).
29+
//DefaultViewportY Y offset in device independent pixels (dip).
2730
const DefaultViewportY = 0
2831

29-
// Rectangle width in device independent pixels (dip).
32+
//DefaultViewportWidth Rectangle width in device independent pixels (dip).
3033
const DefaultViewportWidth = 996
3134

32-
// Rectangle height in device independent pixels (dip).
35+
//DefaultViewportHeight Rectangle height in device independent pixels (dip).
3336
const DefaultViewportHeight = 996
3437

35-
// Page scale factor.
38+
//DefaultViewportScale Page scale factor.
3639
const DefaultViewportScale = 1
3740

38-
// PrintToHtml2Image print page as PDF.
41+
//Html2ImageParams print page as PDF.
3942
type Html2ImageParams struct {
4043
page.CaptureScreenshotParams
41-
CustomClip bool
44+
CustomClip bool
45+
WaitingTime int // Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond
4246
}
4347

48+
//NewDefaultHtml2ImageParams default html convert to image params
4449
func NewDefaultHtml2ImageParams() Html2ImageParams {
4550
return Html2ImageParams{
4651
CustomClip: false,
@@ -56,6 +61,7 @@ func NewDefaultHtml2ImageParams() Html2ImageParams {
5661
},
5762
FromSurface: DefaultFromSurface,
5863
},
64+
WaitingTime: DefaultWaitingTime,
5965
}
6066
}
6167

@@ -82,6 +88,7 @@ func (ins *html2image) Convert() ([]byte, error) {
8288

8389
if err := chromedp.Run(ctx,
8490
chromedp.Navigate(ins.cc.Url),
91+
chromedp.Sleep(time.Duration(params.WaitingTime)*time.Millisecond),
8592
chromedp.ActionFunc(func(ctx context.Context) error {
8693

8794
if !params.CustomClip {

converter/doctron_core/html2pdf.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,70 @@ package doctron_core
33
import (
44
"context"
55
"errors"
6+
"time"
7+
68
"github.com/chromedp/cdproto/page"
79
"github.com/chromedp/chromedp"
8-
"time"
910
)
1011

11-
// Paper orientation. Defaults to false.
12+
//DefaultLandscape Paper orientation. Defaults to false.
1213
const DefaultLandscape = false
1314

14-
// Display header and footer. Defaults to false.
15+
//DefaultDisplayHeaderFooter Display header and footer. Defaults to false.
1516
const DefaultDisplayHeaderFooter = false
1617

17-
// Print background graphics. Defaults to true.
18+
//DefaultPrintBackground Print background graphics. Defaults to true.
1819
const DefaultPrintBackground = true
1920

20-
// Scale of the webpage rendering. Defaults to 1.
21+
//DefaultScale Scale of the webpage rendering. Defaults to 1.
2122
const DefaultScale = 1
2223

23-
// Paper width in inches. Defaults to 8.5 inches.
24+
//DefaultPaperWidth Paper width in inches. Defaults to 8.5 inches.
2425
const DefaultPaperWidth = 8.5
2526

26-
// Paper height in inches. Defaults to 11 inches.
27+
//DefaultPaperHeight Paper height in inches. Defaults to 11 inches.
2728
const DefaultPaperHeight = 11
2829

29-
// Top margin in inches. Defaults to 1cm (~0.4 inches).
30+
//DefaultMarginTop Top margin in inches. Defaults to 1cm (~0.4 inches).
3031
const DefaultMarginTop = 0.4
3132

32-
// Bottom margin in inches. Defaults to 1cm (~0.4 inches).
33+
//DefaultMarginBottom Bottom margin in inches. Defaults to 1cm (~0.4 inches).
3334
const DefaultMarginBottom = 0.4
3435

35-
// Left margin in inches. Defaults to 1cm (~0.4 inches).
36+
//DefaultMarginLeft Left margin in inches. Defaults to 1cm (~0.4 inches).
3637
const DefaultMarginLeft = 0.4
3738

38-
// Right margin in inches. Defaults to 1cm (~0.4 inches).
39+
//DefaultMarginRight Right margin in inches. Defaults to 1cm (~0.4 inches).
3940
const DefaultMarginRight = 0.4
4041

41-
// Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
42+
//DefaultPageRanges Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
4243
const DefaultPageRanges = ""
4344

44-
// Whether to silently ignore invalid but successfully parsed page ranges, such as '3-2'. Defaults to false.
45+
//DefaultIgnoreInvalidPageRanges Whether to silently ignore invalid but successfully parsed page ranges, such as '3-2'. Defaults to false.
4546
const DefaultIgnoreInvalidPageRanges = false
4647

47-
// HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: - date: formatted print date - title: document title - url: document location - pageNumber: current page number - totalPages: total pages in the document For example, <span class=title></span> would generate span containing the title.
48+
//DefaultHeaderTemplate HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: - date: formatted print date - title: document title - url: document location - pageNumber: current page number - totalPages: total pages in the document For example, <span class=title></span> would generate span containing the title.
4849
const DefaultHeaderTemplate = ""
4950

50-
// HTML template for the print footer. Should use the same format as the headerTemplate.
51+
//DefaultFooterTemplate HTML template for the print footer. Should use the same format as the headerTemplate.
5152
const DefaultFooterTemplate = ""
5253

53-
// Whether or not to prefer page size as defined by css. Defaults to false, in which case the content will be scaled to fit the paper size.
54+
//DefaultPreferCSSPageSize Whether or not to prefer page size as defined by css. Defaults to false, in which case the content will be scaled to fit the paper size.
5455
const DefaultPreferCSSPageSize = false
5556

56-
// PrintToPDFParams print page as PDF.
57+
//DefaultWaitingTime Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond
58+
const DefaultWaitingTime = 0
59+
60+
// PDFParams print page as PDF.
5761
type PDFParams struct {
5862
page.PrintToPDFParams
63+
WaitingTime int // Waiting time after the page loaded. Default 0 means not wait. unit:Millisecond
5964
}
6065

66+
//NewDefaultPDFParams default pdf params
6167
func NewDefaultPDFParams() PDFParams {
6268
return PDFParams{
63-
page.PrintToPDFParams{
69+
PrintToPDFParams: page.PrintToPDFParams{
6470
Landscape: DefaultLandscape,
6571
DisplayHeaderFooter: DefaultDisplayHeaderFooter,
6672
PrintBackground: DefaultPrintBackground,
@@ -77,6 +83,7 @@ func NewDefaultPDFParams() PDFParams {
7783
FooterTemplate: DefaultFooterTemplate,
7884
PreferCSSPageSize: DefaultPreferCSSPageSize,
7985
},
86+
WaitingTime: DefaultWaitingTime,
8087
}
8188
}
8289

@@ -103,6 +110,7 @@ func (ins *html2pdf) Convert() ([]byte, error) {
103110

104111
if err := chromedp.Run(ctx,
105112
chromedp.Navigate(ins.cc.Url),
113+
chromedp.Sleep(time.Duration(params.WaitingTime)*time.Millisecond),
106114
chromedp.ActionFunc(func(ctx context.Context) error {
107115
var err error
108116
ins.buf, _, err = params.Do(ctx)

0 commit comments

Comments
 (0)