-
Notifications
You must be signed in to change notification settings - Fork 149
/
json.go
222 lines (191 loc) · 4.64 KB
/
json.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package wkhtmltopdf
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
)
type jsonPDFGenerator struct {
GlobalOptions globalOptions
OutlineOptions outlineOptions
Cover cover
TOC toc
Pages []jsonPage
}
type jsonPage struct {
PageOptions PageOptions
InputFile string
Base64PageData string
}
// ToJSON creates JSON of the complete representation of the PDFGenerator.
// It also saves all pages. For a PageReader page, the content is stored as a Base64 string in the JSON.
func (pdfg *PDFGenerator) ToJSON() ([]byte, error) {
jpdf := &jsonPDFGenerator{
TOC: pdfg.TOC,
Cover: pdfg.Cover,
GlobalOptions: pdfg.globalOptions,
OutlineOptions: pdfg.outlineOptions,
}
for _, p := range pdfg.pages {
jp := jsonPage{
InputFile: p.InputFile(),
}
switch tp := p.(type) {
case *Page:
jp.PageOptions = tp.PageOptions
case *PageReader:
jp.PageOptions = tp.PageOptions
}
if p.Reader() != nil {
buf, err := io.ReadAll(p.Reader())
if err != nil {
return nil, err
}
jp.Base64PageData = base64.StdEncoding.EncodeToString(buf)
}
jpdf.Pages = append(jpdf.Pages, jp)
}
return json.Marshal(jpdf)
}
// NewPDFGeneratorFromJSON creates a new PDFGenerator and restores all the settings and pages
// from a JSON byte slice which should be created using PDFGenerator.ToJSON().
func NewPDFGeneratorFromJSON(jsonReader io.Reader) (*PDFGenerator, error) {
jp := new(jsonPDFGenerator)
err := json.NewDecoder(jsonReader).Decode(jp)
if err != nil {
return nil, fmt.Errorf("error unmarshaling JSON: %s", err)
}
pdfg, err := NewPDFGenerator()
if err != nil {
return nil, fmt.Errorf("error creating PDF generator: %s", err)
}
pdfg.TOC = jp.TOC
pdfg.Cover = jp.Cover
pdfg.globalOptions = jp.GlobalOptions
pdfg.outlineOptions = jp.OutlineOptions
for i, p := range jp.Pages {
if p.Base64PageData == "" {
pdfg.AddPage(&Page{
Input: p.InputFile,
PageOptions: p.PageOptions,
})
continue
}
buf, err := base64.StdEncoding.DecodeString(p.Base64PageData)
if err != nil {
return nil, fmt.Errorf("error decoding base 64 input on page %d: %s", i, err)
}
pdfg.AddPage(&PageReader{
Input: bytes.NewReader(buf),
PageOptions: p.PageOptions,
})
}
return pdfg, nil
}
type jsonBoolOption struct {
Option string
Value bool
}
func (bo *boolOption) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonBoolOption{bo.option, bo.value})
}
func (bo *boolOption) UnmarshalJSON(b []byte) error {
jbo := new(jsonBoolOption)
err := json.Unmarshal(b, jbo)
if err != nil {
return err
}
bo.value = jbo.Value
bo.option = jbo.Option
return nil
}
type jsonStringOption struct {
Option string
Value string
}
func (so *stringOption) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonStringOption{so.option, so.value})
}
func (so *stringOption) UnmarshalJSON(b []byte) error {
jso := new(jsonStringOption)
err := json.Unmarshal(b, jso)
if err != nil {
return err
}
so.value = jso.Value
so.option = jso.Option
return nil
}
type jsonUintOption struct {
Option string
IsSet bool
Value uint
}
func (io *uintOption) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonUintOption{io.option, io.isSet, io.value})
}
func (io *uintOption) UnmarshalJSON(b []byte) error {
jio := new(jsonUintOption)
err := json.Unmarshal(b, jio)
if err != nil {
return err
}
io.value = jio.Value
io.isSet = jio.IsSet
io.option = jio.Option
return nil
}
type jsonFloatOption struct {
Option string
IsSet bool
Value float64
}
func (fo *floatOption) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonFloatOption{fo.option, fo.isSet, fo.value})
}
func (fo *floatOption) UnmarshalJSON(b []byte) error {
jfo := new(jsonFloatOption)
err := json.Unmarshal(b, jfo)
if err != nil {
return err
}
fo.value = jfo.Value
fo.isSet = jfo.IsSet
fo.option = jfo.Option
return nil
}
type jsonMapOption struct {
Option string
Value map[string]string
}
func (mo *mapOption) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonMapOption{mo.option, mo.value})
}
func (mo *mapOption) UnmarshalJSON(b []byte) error {
jmo := new(jsonMapOption)
err := json.Unmarshal(b, jmo)
if err != nil {
return err
}
mo.value = jmo.Value
mo.option = jmo.Option
return nil
}
type jsonSliceOption struct {
Option string
Value []string
}
func (so *sliceOption) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonSliceOption{so.option, so.value})
}
func (so *sliceOption) UnmarshalJSON(b []byte) error {
jso := new(jsonSliceOption)
err := json.Unmarshal(b, jso)
if err != nil {
return err
}
so.value = jso.Value
so.option = jso.Option
return nil
}