-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
66 lines (60 loc) · 1.33 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
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
chromedptool "github.com/yiGmMk/go-pdf/chromedp"
)
const (
defaultUrl = "https://baidu.com"
defaultPath = "./baidu.pdf"
)
var (
// cli app params
url string
path string
pU = &cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Usage: fmt.Sprintf("url to generate pdf,if no url in input,we use as %s default", defaultUrl),
Destination: &url,
}
pP = &cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Usage: fmt.Sprintf("output pdf file path,if no path in input,we use as %s default", defaultPath),
Destination: &path,
}
)
// gen pdf from url
func GenPdf(url, path string) error {
err := chromedptool.ChromedpHandler{}.GenPdf(url, path)
if err != nil {
return err
}
return nil
}
func main() {
app := &cli.App{
Name: "go-pdf-cli",
Usage: "generate pdf from url",
Action: func(c *cli.Context) error {
log.Printf("input,url: %s, path: %s", url, path)
if url == "" {
url = defaultUrl
log.Printf("no url in input,use as %s default", defaultUrl)
}
if path == "" {
path = defaultPath
log.Printf("no path in input,use as %s default", defaultPath)
}
return GenPdf(url, path)
},
Flags: []cli.Flag{pU, pP},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}