-
Notifications
You must be signed in to change notification settings - Fork 1
/
orcgen.go
69 lines (60 loc) · 1.87 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
// Package orcgen generates files from HTML -
// any static webpage can be informed, or even an HTML file.
// The file will be generated according the choosen extension.
package orcgen
import (
"github.com/luabagg/orcgen/internal"
"github.com/luabagg/orcgen/pkg/director"
)
// Valid extension types constants.
const (
// PDF const.
PDF = internal.PDF
// PNG const.
PNG = internal.PNG
// JPEG const.
JPEG = internal.JPEG
)
// New starts a new Director - the Director contains the available methods for file conversion.
//
// ext is the extension to be converted to (use the defined constants above).
//
// Connect and Close are used for the Browser connection control.
// ConvertWebpage and ConvertHTML are used for page conversion.
//
// There are a set of setters for specific config.
func New(ext internal.Ext) *director.Director {
return director.NewDirector(ext).Connect()
}
// ConvertWebpage converts the url to the ext format, and saves the file.
//
// ext is the extension to be converted to (use the defined constants above).
// url is the url to convert.
// output is a filepath containing the extension.
//
// The connection is automatically closed.
func ConvertWebpage(ext internal.Ext, url string, output string) error {
d := New(ext)
defer d.Close()
fi, err := d.ConvertWebpage(url)
if err != nil {
return err
}
return fi.Output(output)
}
// ConvertHTML converts the informed bytes to the ext format, and saves the file.
//
// ext is the extension to be converted to (use the defined constants above).
// html is the html byte array (if it's a filepath, use os.ReadFile(filepath)).
// output is a filepath containing the extension.
//
// The connection is automatically closed.
func ConvertHTML(ext internal.Ext, html []byte, output string) error {
d := New(ext)
defer d.Close()
fi, err := d.ConvertHTML(html)
if err != nil {
return err
}
return fi.Output(output)
}