From 525c5dd7455dfed882a7bd4f5a5b2aff87645353 Mon Sep 17 00:00:00 2001 From: John Fercher Date: Sat, 23 Sep 2023 10:24:58 -0300 Subject: [PATCH 1/2] Add metadatas --- Makefile | 1 + README.md | 2 +- docs/README.md | 4 +- docs/_sidebar.md | 1 + docs/assets/examples/metadatas/v2/main.go | 42 +++++++++ docs/assets/pdf/metadatasv2.pdf | Bin 0 -> 1180 bytes docs/assets/text/metadatasv2.txt | 3 + docs/v2/features/metadatas.md | 12 +++ pkg/config/builder.go | 103 ++++++++++++++++++---- pkg/config/config.go | 1 + pkg/config/metadata.go | 16 ++++ pkg/core/provider.go | 1 + pkg/maroto.go | 5 +- pkg/providers/gofpdf/gofpdf.go | 31 ++++++- pkg/providers/html/html.go | 4 + 15 files changed, 203 insertions(+), 23 deletions(-) create mode 100644 docs/assets/examples/metadatas/v2/main.go create mode 100644 docs/assets/pdf/metadatasv2.pdf create mode 100644 docs/assets/text/metadatasv2.txt create mode 100644 docs/v2/features/metadatas.md create mode 100644 pkg/config/metadata.go diff --git a/Makefile b/Makefile index f439eb1a..8241ce7e 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,7 @@ examples: font go run docs/assets/examples/list/v2/main.go go run docs/assets/examples/margins/v2/main.go go run docs/assets/examples/maxgridsum/v2/main.go + go run docs/assets/examples/metadatas/v2/main.go go run docs/assets/examples/orientation/v2/main.go go run docs/assets/examples/pagenumber/v2/main.go go run docs/assets/examples/parallelism/v2/main.go diff --git a/README.md b/README.md index b703d91b..38a32a74 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ always when a new page appear, in this case, a header may have many rows, lines * With `go get`: ```bash -go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.17 +go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.18 ``` diff --git a/docs/README.md b/docs/README.md index 574c60ce..add80ac0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,12 +2,12 @@ [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#template-engines) [![Roadmap](https://img.shields.io/badge/V2-Roadmap-purple)](https://github.com/users/johnfercher/projects/1) [![Discussion](https://img.shields.io/badge/V2-Discussion-blue)](https://github.com/johnfercher/maroto/issues/257) [![Release Notes](https://img.shields.io/badge/Release-Notes-cyan)](https://github.com/johnfercher/maroto/releases) [![Visits Badge](https://badges.pufler.dev/visits/johnfercher/maroto)](https://badges.pufler.dev) -#### Maroto`v2.0.0-alpha.17`is here! Try out: +#### Maroto`v2.0.0-alpha.18`is here! Try out: * Installation with`go get`: ```bash -go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.17 +go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.18 ``` The public API was completely redesigned with the aim of enhancing the diff --git a/docs/_sidebar.md b/docs/_sidebar.md index b42ca736..d93d3174 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -17,6 +17,7 @@ * [List](v2/features/list.md?id=list) * [Margins](v2/features/margins.md?id=custom-margins) * [Max Grid Sum](v2/features/maxgridsum.md?id=max-grid-sum) + * [Metadatas](v2/features/metadatas.md?id=metadatas) * [Orientation](v2/features/orientation.md?id=orientation) * [Page Number](v2/features/pagenumber.md?id=page-number) * [Parallelism](v2/features/parallelism.md?id=parallelism) diff --git a/docs/assets/examples/metadatas/v2/main.go b/docs/assets/examples/metadatas/v2/main.go new file mode 100644 index 00000000..0aec080a --- /dev/null +++ b/docs/assets/examples/metadatas/v2/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "log" + "time" + + "github.com/johnfercher/maroto/v2/pkg" + "github.com/johnfercher/maroto/v2/pkg/components/text" + "github.com/johnfercher/maroto/v2/pkg/config" +) + +func main() { + cfg := config.NewBuilder(). + WithAuthor("author", false). + WithCreator("creator", false). + WithSubject("subject", false). + WithTitle("title", false). + WithCreationDate(time.Now()). + Build() + + mrt := pkg.NewMaroto(cfg) + m := pkg.NewMetricsDecorator(mrt) + + m.AddRows( + text.NewRow(30, "metadatas"), + ) + + document, err := m.Generate() + if err != nil { + log.Fatal(err.Error()) + } + + err = document.Save("docs/assets/pdf/metadatasv2.pdf") + if err != nil { + log.Fatal(err.Error()) + } + + err = document.GetReport().Save("docs/assets/text/metadatasv2.txt") + if err != nil { + log.Fatal(err.Error()) + } +} diff --git a/docs/assets/pdf/metadatasv2.pdf b/docs/assets/pdf/metadatasv2.pdf new file mode 100644 index 0000000000000000000000000000000000000000..ab5060e1b60c1a7b131d41e028d009687732647c GIT binary patch literal 1180 zcmbtUZEw>s5bkIHiu*FD`#|ixxJghoX?=qMV^tRkq-r1B_}Zmv<0)}r@Z*7VlCseW z35lr9+0WVc^4wj{=9Aeg-wRm?0?ca5hC`lxdqCiGwLwPj3QG$qfU-Hc}1Bt?uSEK{Bwm|G7IONf4*65F}FbX z-F9qtrn-GlId;es0fRpS)Q21^=>PcHoAu_s3B94`_q<+w&UjYX9YWtaL0h7u@zJl_ z1woewdvEBvY9s+;YD4Rgyj>ij!juzb$=Kv|Ac9b&LFkLXk9<0CZS>!CPhrQ;!fxki zO#QPn8#hWD8k}yDKB_%!9F5cc3bjT(D|QV6ywxok0J{S+-Tu0Md#rFxvy(6yneKss zl%a$*9I-GUFYbZzHzozmdol#p=*(SF%lI-kkdNX6c0QxF6dH?_b literal 0 HcmV?d00001 diff --git a/docs/assets/text/metadatasv2.txt b/docs/assets/text/metadatasv2.txt new file mode 100644 index 00000000..45677207 --- /dev/null +++ b/docs/assets/text/metadatasv2.txt @@ -0,0 +1,3 @@ +generate -> avg: 237.51μs, executions: [237.51μs] +add_row -> avg: 276.00ns, executions: [276.00ns] +file_size -> 1.18Kb diff --git a/docs/v2/features/metadatas.md b/docs/v2/features/metadatas.md new file mode 100644 index 00000000..4dd2c8fe --- /dev/null +++ b/docs/v2/features/metadatas.md @@ -0,0 +1,12 @@ +# Metadatas + +## Code Example +[filename](../../assets/examples/metadatas/v2/main.go ':include :type=code') + +## PDF Generated +```pdf + assets/pdf/metadatasv2.pdf +``` + +## Time Execution +[filename](../../assets/text/metadatasv2.txt ':include :type=code') \ No newline at end of file diff --git a/pkg/config/builder.go b/pkg/config/builder.go index 17b29159..eb29cdaf 100644 --- a/pkg/config/builder.go +++ b/pkg/config/builder.go @@ -2,6 +2,7 @@ package config import ( "strings" + "time" "github.com/johnfercher/maroto/v2/pkg/consts/orientation" @@ -14,22 +15,6 @@ import ( "github.com/johnfercher/maroto/v2/pkg/props" ) -type builder struct { - providerType provider.Type - dimensions *Dimensions - margins *Margins - workerPoolSize int - debug bool - maxGridSize int - font *props.Font - customFonts []*CustomFont - pageNumberPattern string - pageNumberPlace props.Place - protection *Protection - compression bool - orientation orientation.Type -} - type Builder interface { WithPageSize(size pagesize.Type) Builder WithDimensions(dimensions *Dimensions) Builder @@ -44,9 +29,31 @@ type Builder interface { WithProtection(protectionType protection.Type, userPassword, ownerPassword string) Builder WithCompression(compression bool) Builder WithOrientation(orientation orientation.Type) Builder + WithAuthor(author string, isUTF8 bool) Builder + WithCreator(creator string, isUTF8 bool) Builder + WithSubject(subject string, isUTF8 bool) Builder + WithTitle(title string, isUTF8 bool) Builder + WithCreationDate(time time.Time) Builder Build() *Config } +type builder struct { + providerType provider.Type + dimensions *Dimensions + margins *Margins + workerPoolSize int + debug bool + maxGridSize int + font *props.Font + customFonts []*CustomFont + pageNumberPattern string + pageNumberPlace props.Place + protection *Protection + compression bool + orientation orientation.Type + metadata *Metadata +} + func NewBuilder() Builder { return &builder{ providerType: provider.Gofpdf, @@ -62,6 +69,7 @@ func NewBuilder() Builder { Style: fontstyle.Normal, Color: props.NewBlack(), }, + metadata: &Metadata{}, } } @@ -227,6 +235,68 @@ func (b *builder) WithOrientation(orientation orientation.Type) Builder { return b } +func (b *builder) WithAuthor(author string, isUTF8 bool) Builder { + if author == "" { + return b + } + + b.metadata.Author = &Utf8Text{ + Text: author, + UTF8: isUTF8, + } + + return b +} + +func (b *builder) WithCreator(creator string, isUTF8 bool) Builder { + if creator == "" { + return b + } + + b.metadata.Creator = &Utf8Text{ + Text: creator, + UTF8: isUTF8, + } + + return b +} + +func (b *builder) WithSubject(subject string, isUTF8 bool) Builder { + if subject == "" { + return b + } + + b.metadata.Subject = &Utf8Text{ + Text: subject, + UTF8: isUTF8, + } + + return b +} + +func (b *builder) WithTitle(title string, isUTF8 bool) Builder { + if title == "" { + return b + } + + b.metadata.Title = &Utf8Text{ + Text: title, + UTF8: isUTF8, + } + + return b +} + +func (b *builder) WithCreationDate(time time.Time) Builder { + if time.IsZero() { + return b + } + + b.metadata.CreationDate = time + + return b +} + func (b *builder) Build() *Config { return &Config{ ProviderType: b.providerType, @@ -241,6 +311,7 @@ func (b *builder) Build() *Config { PageNumberPlace: b.pageNumberPlace, Protection: b.protection, Compression: b.compression, + Metadata: b.metadata, } } diff --git a/pkg/config/config.go b/pkg/config/config.go index e3a8d12a..96a93c2a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,4 +18,5 @@ type Config struct { PageNumberPlace props.Place Protection *Protection Compression bool + Metadata *Metadata } diff --git a/pkg/config/metadata.go b/pkg/config/metadata.go new file mode 100644 index 00000000..723f50bd --- /dev/null +++ b/pkg/config/metadata.go @@ -0,0 +1,16 @@ +package config + +import "time" + +type Metadata struct { + Author *Utf8Text + Creator *Utf8Text + Subject *Utf8Text + Title *Utf8Text + CreationDate time.Time +} + +type Utf8Text struct { + Text string + UTF8 bool +} diff --git a/pkg/core/provider.go b/pkg/core/provider.go index 4081cb31..eba98404 100644 --- a/pkg/core/provider.go +++ b/pkg/core/provider.go @@ -29,4 +29,5 @@ type Provider interface { SetProtection(protection *config.Protection) SetCompression(compression bool) + SetMetadata(metadata *config.Metadata) } diff --git a/pkg/maroto.go b/pkg/maroto.go index 17f73a22..d31d7fc3 100644 --- a/pkg/maroto.go +++ b/pkg/maroto.go @@ -120,10 +120,9 @@ func (m *maroto) RegisterFooter(rows ...core.Row) error { } func (m *maroto) Generate() (core.Document, error) { - if m.config.Protection != nil { - m.provider.SetProtection(m.config.Protection) - } + m.provider.SetProtection(m.config.Protection) m.provider.SetCompression(m.config.Compression) + m.provider.SetMetadata(m.config.Metadata) m.fillPageToAddNew() m.setConfig() diff --git a/pkg/providers/gofpdf/gofpdf.go b/pkg/providers/gofpdf/gofpdf.go index a60421ed..bfb56f70 100644 --- a/pkg/providers/gofpdf/gofpdf.go +++ b/pkg/providers/gofpdf/gofpdf.go @@ -2,7 +2,6 @@ package gofpdf import ( "bytes" - "github.com/johnfercher/maroto/v2/internal" "github.com/johnfercher/maroto/v2/pkg/cache" "github.com/johnfercher/maroto/v2/pkg/config" @@ -142,9 +141,39 @@ func (g *gofpdfProvider) CreateRow(height float64) { } func (g *gofpdfProvider) SetProtection(protection *config.Protection) { + if protection == nil { + return + } + g.fpdf.SetProtection(byte(protection.Type), protection.UserPassword, protection.OwnerPassword) } +func (g *gofpdfProvider) SetMetadata(metadata *config.Metadata) { + if metadata == nil { + return + } + + if metadata.Author != nil { + g.fpdf.SetAuthor(metadata.Author.Text, metadata.Author.UTF8) + } + + if metadata.Creator != nil { + g.fpdf.SetCreator(metadata.Creator.Text, metadata.Creator.UTF8) + } + + if metadata.Subject != nil { + g.fpdf.SetSubject(metadata.Subject.Text, metadata.Subject.UTF8) + } + + if metadata.Title != nil { + g.fpdf.SetTitle(metadata.Title.Text, metadata.Title.UTF8) + } + + if !metadata.CreationDate.IsZero() { + g.fpdf.SetCreationDate(metadata.CreationDate) + } +} + func (g *gofpdfProvider) GenerateFile(file string) error { return g.fpdf.OutputFileAndClose(file) } diff --git a/pkg/providers/html/html.go b/pkg/providers/html/html.go index 8c81be71..0aa38c62 100644 --- a/pkg/providers/html/html.go +++ b/pkg/providers/html/html.go @@ -122,6 +122,10 @@ func (h *html) SetCache(cache cache.Cache) { func (h *html) SetProtection(_ *config.Protection) { } +func (h *html) SetMetadata(_ *config.Metadata) { + +} + func (h *html) CreateCol(width, height float64, config *config.Config, prop *props.Cell) { var row *tree.Node[Div] From 40e64d5fc6cff6cf0d56286422c75d23f54aa74f Mon Sep 17 00:00:00 2001 From: John Fercher Date: Sat, 23 Sep 2023 10:29:20 -0300 Subject: [PATCH 2/2] Add metadatas --- pkg/providers/gofpdf/gofpdf.go | 1 + pkg/providers/html/html.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/providers/gofpdf/gofpdf.go b/pkg/providers/gofpdf/gofpdf.go index bfb56f70..362888b9 100644 --- a/pkg/providers/gofpdf/gofpdf.go +++ b/pkg/providers/gofpdf/gofpdf.go @@ -2,6 +2,7 @@ package gofpdf import ( "bytes" + "github.com/johnfercher/maroto/v2/internal" "github.com/johnfercher/maroto/v2/pkg/cache" "github.com/johnfercher/maroto/v2/pkg/config" diff --git a/pkg/providers/html/html.go b/pkg/providers/html/html.go index 0aa38c62..49ae407c 100644 --- a/pkg/providers/html/html.go +++ b/pkg/providers/html/html.go @@ -123,7 +123,6 @@ func (h *html) SetProtection(_ *config.Protection) { } func (h *html) SetMetadata(_ *config.Metadata) { - } func (h *html) CreateCol(width, height float64, config *config.Config, prop *props.Cell) {