/
headless-chromium.go
147 lines (113 loc) · 3.29 KB
/
headless-chromium.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package headlesschromium
import (
"bytes"
"context"
"errors"
"io"
"time"
"github.com/lucas-gaitzsch/pdf-turtle/config"
"github.com/rs/zerolog/log"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
//TODO: strip html with: <script\b[^>]*>([\s\S]*?)<\/script>
func NewChromiumBrowser(ctx context.Context) (context.Context, context.CancelFunc) {
opts := chromedp.DefaultExecAllocatorOptions[:]
opts = append(
opts,
chromedp.Headless,
chromedp.Flag("headless", true),
chromedp.Flag("hide-scrollbars", true),
chromedp.Flag("mute-audio", true),
)
if (config.Get(ctx).NoSandbox) {
opts = append(
opts,
chromedp.Flag("no-sandbox", true),
)
}
allocCtx, cancelAllocCtx := chromedp.NewExecAllocator(ctx, opts...)
cctx, cancelCctx := chromedp.NewContext(allocCtx)
// Keep chromium browser process running
if err := chromedp.Run(cctx); err != nil {
log.Error().Err(err).Msg("chromium browser could not be initialized")
panic(err)
}
log.Info().Msg("new browser initialized")
cancel := func() {
log.Info().Msg("cancel called: close chromium")
cancelCctx()
cancelAllocCtx()
}
return cctx, cancel
}
type OptionsConfigureFunc func(params *page.PrintToPDFParams) *page.PrintToPDFParams
func RenderHtmlAsPdf(chromiumAllocCtx context.Context, outerCtx context.Context, location string, html *string, optionsConfigureFunc OptionsConfigureFunc) (io.Reader, error) {
if html == nil && location == "" {
return nil, errors.New("html is nil")
}
if location == "" {
location = "about:blank"
}
cctx, cancel := chromedp.NewContext(chromiumAllocCtx)
defer cancel()
go func() {
select {
case <-cctx.Done():
case <-outerCtx.Done():
log.Info().Msg("cancel chromium pdf rendering by outer context")
cancel()
}
}()
var pdfStream io.Reader
tasks := chromedp.Tasks{
chromedp.Navigate(location),
chromedp.ActionFunc(func(cctx context.Context) error {
lctx, cancelLctx := context.WithCancel(cctx)
defer cancelLctx()
done := make(chan bool, 1)
chromedp.ListenTarget(lctx, func(ev any) {
if _, ok := ev.(*page.EventLoadEventFired); ok {
cancelLctx()
done <- true
}
})
frameTree, err := page.GetFrameTree().Do(cctx)
if err != nil {
return err
}
if html != nil {
if err := page.SetDocumentContent(frameTree.Frame.ID, *html).Do(cctx); err != nil {
return err
}
}
select {
case <-done:
return nil
case <-time.After(time.Duration(config.Get(outerCtx).RenderTimeoutInSeconds) * time.Second):
return errors.New("render timeout")
case <-outerCtx.Done():
return errors.New("canceled by outer ctx")
}
}),
// injectCss(preloadedMergedCss),
chromedp.ActionFunc(func(cctx context.Context) error {
params := page.PrintToPDF()
b, _, err := optionsConfigureFunc(params).Do(cctx)
pdfStream = bytes.NewReader(b)
return err
}),
}
if err := chromedp.Run(cctx, runWithTimeOut(outerCtx, tasks)); err != nil {
return nil, err
}
return pdfStream, nil
}
func runWithTimeOut(outerCtx context.Context, tasks chromedp.Tasks) chromedp.ActionFunc {
timeout := time.Duration(config.Get(outerCtx).RenderTimeoutInSeconds) * time.Second
return func(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return tasks.Do(timeoutCtx)
}
}