-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathmaroto.go
389 lines (314 loc) · 9.79 KB
/
maroto.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package maroto
import (
"errors"
"github.com/f-amaral/go-async/pool"
"github.com/johnfercher/maroto/v2/pkg/consts/generation"
"github.com/johnfercher/maroto/v2/internal/cache"
"github.com/johnfercher/maroto/v2/internal/providers/gofpdf"
"github.com/johnfercher/maroto/v2/pkg/merge"
"github.com/johnfercher/maroto/v2/pkg/core/entity"
"github.com/johnfercher/go-tree/node"
"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/components/page"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/config"
"github.com/johnfercher/maroto/v2/pkg/core"
)
type Maroto struct {
config *entity.Config
provider core.Provider
cache cache.Cache
// Building
cell entity.Cell
pages []core.Page
rows []core.Row
header []core.Row
footer []core.Row
headerHeight float64
footerHeight float64
currentHeight float64
}
// GetCurrentConfig is responsible for returning the current settings from the file
func (m *Maroto) GetCurrentConfig() *entity.Config {
return m.config
}
// New is responsible for create a new instance of core.Maroto.
// It's optional to provide an *entity.Config with customizations
// those customization are created by using the config.Builder.
func New(cfgs ...*entity.Config) core.Maroto {
cache := cache.New()
cfg := getConfig(cfgs...)
provider := getProvider(cache, cfg)
m := &Maroto{
provider: provider,
cell: entity.NewRootCell(cfg.Dimensions.Width, cfg.Dimensions.Height, entity.Margins{
Left: cfg.Margins.Left,
Top: cfg.Margins.Top,
Right: cfg.Margins.Right,
Bottom: cfg.Margins.Bottom,
}),
cache: cache,
config: cfg,
}
return m
}
// AddPages is responsible for add pages directly in the document.
// By adding a page directly, the current cursor will reset and the
// new page will appear as the next. If the page provided have
// more rows than the maximum useful area of a page, maroto will split
// that page in more than one.
func (m *Maroto) AddPages(pages ...core.Page) {
for _, page := range pages {
if m.currentHeight != m.headerHeight {
m.fillPageToAddNew()
m.addHeader()
}
m.addRows(page.GetRows()...)
}
}
// AddRows is responsible for add rows in the current document.
// By adding a row, if the row will extrapolate the useful area of a page,
// maroto will automatically add a new page. Maroto use the information of
// PageSize, PageMargin, FooterSize and HeaderSize to calculate the useful
// area of a page.
func (m *Maroto) AddRows(rows ...core.Row) {
m.addRows(rows...)
}
// AddRow is responsible for add one row in the current document.
// By adding a row, if the row will extrapolate the useful area of a page,
// maroto will automatically add a new page. Maroto use the information of
// PageSize, PageMargin, FooterSize and HeaderSize to calculate the useful
// area of a page.
func (m *Maroto) AddRow(rowHeight float64, cols ...core.Col) core.Row {
r := row.New(rowHeight).Add(cols...)
m.addRow(r)
return r
}
// AddAutoRow is responsible for adding a line with automatic height to the
// current document.
// The row height will be calculated based on its content.
func (m *Maroto) AddAutoRow(cols ...core.Col) core.Row {
r := row.New().Add(cols...)
m.addRow(r)
return r
}
// FitlnCurrentPage is responsible to validating whether a line fits on
// the current page.
func (m *Maroto) FitlnCurrentPage(heightNewLine float64) bool {
contentSize := m.getRowsHeight(m.rows...) + m.footerHeight + m.headerHeight
return contentSize+heightNewLine < m.config.Dimensions.Height
}
// RegisterHeader is responsible to define a set of rows as a header
// of the document. The header will appear in every new page of the document.
// The header cannot occupy an area greater than the useful area of the page,
// it this case the method will return an error.
func (m *Maroto) RegisterHeader(rows ...core.Row) error {
height := m.getRowsHeight(rows...)
if height+m.footerHeight > m.config.Dimensions.Height {
return errors.New("header height is greater than page useful area")
}
m.headerHeight = height
m.header = rows
for _, headerRow := range rows {
m.addRow(headerRow)
}
return nil
}
// RegisterFooter is responsible to define a set of rows as a footer
// of the document. The footer will appear in every new page of the document.
// The footer cannot occupy an area greater than the useful area of the page,
// it this case the method will return an error.
func (m *Maroto) RegisterFooter(rows ...core.Row) error {
height := m.getRowsHeight(rows...)
if height > m.config.Dimensions.Height {
return errors.New("footer height is greater than page useful area")
}
m.footerHeight = height
m.footer = rows
return nil
}
// Generate is responsible to compute the component tree created by
// the usage of all other Maroto methods, and generate the PDF document.
func (m *Maroto) Generate() (core.Document, error) {
m.fillPageToAddNew()
m.setConfig()
if m.config.GenerationMode == generation.Concurrent {
return m.generateConcurrently()
}
if m.config.GenerationMode == generation.SequentialLowMemory {
return m.generateLowMemory()
}
return m.generate()
}
// GetStructure is responsible for return the component tree, this is useful
// on unit tests cases.
func (m *Maroto) GetStructure() *node.Node[core.Structure] {
m.fillPageToAddNew()
str := core.Structure{
Type: "maroto",
Details: m.config.ToMap(),
}
node := node.New(str)
for _, p := range m.pages {
inner := p.GetStructure()
node.AddNext(inner)
}
return node
}
func (m *Maroto) addRows(rows ...core.Row) {
for _, row := range rows {
m.addRow(row)
}
}
func (m *Maroto) addRow(r core.Row) {
if len(r.GetColumns()) == 0 {
r.Add(col.New())
}
maxHeight := m.cell.Height
r.SetConfig(m.config)
rowHeight := r.GetHeight(m.provider, &m.cell)
sumHeight := rowHeight + m.currentHeight + m.footerHeight
// Row smaller than the remain space on page
if sumHeight < maxHeight {
m.currentHeight += rowHeight
m.rows = append(m.rows, r)
return
}
// As row will extrapolate page, we will add empty space
// on the page to force a new page
m.fillPageToAddNew()
m.addHeader()
// AddRows row on the new page
m.currentHeight += rowHeight
m.rows = append(m.rows, r)
}
func (m *Maroto) addHeader() {
for _, headerRow := range m.header {
m.currentHeight += headerRow.GetHeight(m.provider, &m.cell)
m.rows = append(m.rows, headerRow)
}
}
func (m *Maroto) fillPageToAddNew() {
space := m.cell.Height - m.currentHeight - m.footerHeight
c := col.New(m.config.MaxGridSize)
spaceRow := row.New(space)
spaceRow.Add(c)
m.rows = append(m.rows, spaceRow)
m.rows = append(m.rows, m.footer...)
var p core.Page
if m.config.PageNumber != nil {
p = page.New(*m.config.PageNumber)
} else {
p = page.New()
}
p.SetConfig(m.config)
p.Add(m.rows...)
m.pages = append(m.pages, p)
m.rows = nil
m.currentHeight = 0
}
func (m *Maroto) setConfig() {
for i, page := range m.pages {
page.SetConfig(m.config)
page.SetNumber(i+1, len(m.pages))
}
}
func (m *Maroto) generate() (core.Document, error) {
innerCtx := m.cell.Copy()
for _, page := range m.pages {
page.Render(m.provider, innerCtx)
}
documentBytes, err := m.provider.GenerateBytes()
if err != nil {
return nil, err
}
return core.NewPDF(documentBytes, nil), nil
}
func (m *Maroto) generateConcurrently() (core.Document, error) {
p := pool.NewPool[[]core.Page, []byte](m.config.ChunkWorkers, m.processPage,
pool.WithSortingOutput[[]core.Page, []byte]())
defer p.Close()
chunks := len(m.pages) / m.config.ChunkWorkers
if chunks == 0 {
chunks = 1
}
pageGroups := make([][]core.Page, 0)
for i := 0; i < len(m.pages); i += chunks {
end := i + chunks
if end > len(m.pages) {
end = len(m.pages)
}
pageGroups = append(pageGroups, m.pages[i:end])
}
processed := p.Process(pageGroups)
if processed.HasError {
return nil, errors.New("an error has occurred while trying to generate PDFs concurrently")
}
pdfs := make([][]byte, len(processed.Results))
for i, result := range processed.Results {
bytes := result.Output.([]byte)
pdfs[i] = bytes
}
mergedBytes, err := merge.Bytes(pdfs...)
if err != nil {
return nil, err
}
return core.NewPDF(mergedBytes, nil), nil
}
func (m *Maroto) generateLowMemory() (core.Document, error) {
chunks := len(m.pages) / m.config.ChunkWorkers
if chunks == 0 {
chunks = 1
}
pageGroups := make([][]core.Page, 0)
for i := 0; i < len(m.pages); i += chunks {
end := i + chunks
if end > len(m.pages) {
end = len(m.pages)
}
pageGroups = append(pageGroups, m.pages[i:end])
}
var pdfResults [][]byte
for _, pageGroup := range pageGroups {
bytes, err := m.processPage(pageGroup)
if err != nil {
return nil, errors.New("an error has occurred while trying to generate PDFs in low memory mode")
}
pdfResults = append(pdfResults, bytes)
}
mergedBytes, err := merge.Bytes(pdfResults...)
if err != nil {
return nil, err
}
return core.NewPDF(mergedBytes, nil), nil
}
func (m *Maroto) processPage(pages []core.Page) ([]byte, error) {
innerCtx := m.cell.Copy()
innerProvider := getProvider(cache.NewMutexDecorator(cache.New()), m.config)
for _, page := range pages {
page.Render(innerProvider, innerCtx)
}
return innerProvider.GenerateBytes()
}
func (m *Maroto) getRowsHeight(rows ...core.Row) float64 {
var height float64
for _, r := range rows {
r.SetConfig(m.config)
height += r.GetHeight(m.provider, &m.cell)
}
return height
}
func getConfig(configs ...*entity.Config) *entity.Config {
if len(configs) > 0 {
return configs[0]
}
return config.NewBuilder().Build()
}
func getProvider(cache cache.Cache, cfg *entity.Config) core.Provider {
deps := gofpdf.NewBuilder().Build(cfg, cache)
provider := gofpdf.New(deps)
provider.SetMetadata(cfg.Metadata)
provider.SetCompression(cfg.Compression)
provider.SetProtection(cfg.Protection)
return provider
}