-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
82 lines (70 loc) · 1.73 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/json"
"log"
"os"
"strings"
pdf "github.com/adrg/go-wkhtmltopdf"
)
// For the full list of options, see pkg.go.dev/github.com/adrg/go-wkhtmltopdf.
// NOTE: pdf.ConverterOpts and pdf.ObjectOpts also support YAML unmarshalling.
var jsonInput = strings.NewReader(`{
"converterOpts": {
"title": "google.com",
"paperSize": "A4",
"orientation": "Portrait",
"marginLeft": "10mm",
"marginRight": "10mm"
},
"objectOpts": {
"location": "https://google.com",
"footer": {
"contentCenter": "[page]",
"fontSize": 14
}
}
}`)
type inputData struct {
ConverterOpts *pdf.ConverterOpts `json:"converterOpts"`
ObjectOpts *pdf.ObjectOpts `json:"objectOpts"`
}
func main() {
// Initialize library.
if err := pdf.Init(); err != nil {
log.Fatal(err)
}
defer pdf.Destroy()
// Set default options. Any option fields specified in the JSON
// input data will overwrite the defaults.
input := &inputData{
ConverterOpts: pdf.NewConverterOpts(),
ObjectOpts: pdf.NewObjectOpts(),
}
if err := json.NewDecoder(jsonInput).Decode(input); err != nil {
log.Fatal(err)
}
// Create object.
object, err := pdf.NewObjectWithOpts(input.ObjectOpts)
if err != nil {
log.Fatal(err)
}
// Create converter.
converter, err := pdf.NewConverterWithOpts(input.ConverterOpts)
if err != nil {
log.Fatal(err)
}
defer converter.Destroy()
// Add object to the converter.
converter.Add(object)
// Create output file.
outFile, err := os.Create("out.pdf")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
// Run converter. Due to a limitation of the `wkhtmltox` library, the
// conversion must be performed on the main thread.
if err := converter.Run(outFile); err != nil {
log.Fatal(err)
}
}