-
Notifications
You must be signed in to change notification settings - Fork 4
/
orcgen.go
92 lines (77 loc) · 2.86 KB
/
orcgen.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
// Package orcgen generates files from HTML / URLs -
// any webpage can be informed, or even an HTML file.
//
// The file will be generated accordingly the configured handler.
// You can also configure the webdriver to control the page before saving the file.
package orcgen
import (
"github.com/go-rod/rod/lib/proto"
"github.com/luabagg/orcgen/v2/pkg/fileinfo"
"github.com/luabagg/orcgen/v2/pkg/handlers"
"github.com/luabagg/orcgen/v2/pkg/handlers/pdf"
"github.com/luabagg/orcgen/v2/pkg/handlers/screenshot"
"github.com/luabagg/orcgen/v2/pkg/webdriver"
)
// Aliases:
type ScreenshotConfig = proto.PageCaptureScreenshot
type PDFConfig = proto.PagePrintToPDF
// Generate generates a file from the given HTML / URL and outputs it to the given path.
//
// There's no checking in the extension type, so make sure to use the correct one.
func Generate[T string | []byte, Config handlers.Config](html T, config Config, output string) error {
handler := NewHandler(config)
var fileinfo *fileinfo.Fileinfo
var err error
if _, ok := any(html).([]byte); ok {
fileinfo, err = ConvertHTML(handler, any(html).([]byte))
} else {
fileinfo, err = ConvertWebpage(handler, any(html).(string))
}
if err != nil {
return err
}
return fileinfo.Output(output)
}
// NewHandler creates a handler from the config.
//
// It checks the config type and instanciates the handler accordingly.
func NewHandler[Config handlers.Config](config Config) handlers.FileHandler[Config] {
var handler any
if _, ok := any(config).(PDFConfig); ok {
handler = pdf.New()
} else if _, ok := any(config).(ScreenshotConfig); ok {
handler = screenshot.New()
} else {
panic("invalid config type provided")
}
return any(handler).(handlers.FileHandler[Config]).SetConfig(config)
}
// ConvertHTML converts the bytes using the given handler, and returns a Fileinfo object.
//
// handler is a Handler instance (see pkg/handlers).
// html is the html byte array (if it's a filepath, use os.ReadFile(filepath)).
//
// The connection with the Browser is automatically closed.
func ConvertHTML[Config handlers.Config](handler handlers.FileHandler[Config], html []byte) (*fileinfo.Fileinfo, error) {
wd := webdriver.FromDefault()
defer wd.Close()
page, err := wd.HTMLToPage(html)
if err != nil {
return nil, err
}
wd.WaitLoad(page)
return handler.GenerateFile(page)
}
// ConvertWebpage converts the url using the given handler, and returns a Fileinfo object
//
// handler is a Handler instance (see pkg/handlers).
// url will be converted as configured, if you need special treats, check the Webdriver docs.
//
// The connection with the Browser is automatically closed.
func ConvertWebpage[Config handlers.Config](handler handlers.FileHandler[Config], url string) (*fileinfo.Fileinfo, error) {
wd := webdriver.FromDefault()
defer wd.Close()
page := wd.UrlToPage(url)
wd.WaitLoad(page)
return handler.GenerateFile(page)
}