/
main.go
67 lines (58 loc) · 1.19 KB
/
main.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
package main
import (
"context"
"io/ioutil"
"os"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/runner"
)
func main() {
// create context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create chrome instance
c, err := chromedp.New(ctx,
chromedp.WithRunnerOptions(
runner.Flag("headless", ""),
runner.Flag("no-sandbox", ""),
runner.Flag("no-gpu", ""),
),
)
if err != nil {
panic(err)
}
// run task list
var pdfBuf []byte
err = c.Run(ctx, chromedp.Tasks{
chromedp.Navigate("http://localhost:3000"),
chromedp.Sleep(2 * time.Second),
chromedp.ActionFunc(func(ctx context.Context, h cdp.Executor) error {
printToPDF := &page.PrintToPDFParams{
PrintBackground: true,
PreferCSSPageSize: true,
}
// take page screenshot
buf, err := printToPDF.Do(ctx, h)
if err != nil {
return err
}
pdfBuf = buf
return nil
}),
})
if err != nil {
panic(err)
}
err = ioutil.WriteFile("static/resume.pdf", pdfBuf, os.ModePerm)
if err != nil {
panic(err)
}
// shutdown chrome
err = c.Shutdown(ctx)
if err != nil {
panic(err)
}
}