Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadatas #306

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions docs/assets/examples/metadatas/v2/main.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
Binary file added docs/assets/pdf/metadatasv2.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions docs/assets/text/metadatasv2.txt
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions docs/v2/features/metadatas.md
Original file line number Diff line number Diff line change
@@ -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')
103 changes: 87 additions & 16 deletions pkg/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"strings"
"time"

"github.com/johnfercher/maroto/v2/pkg/consts/orientation"

Expand All @@ -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
Expand All @@ -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,
Expand All @@ -62,6 +69,7 @@ func NewBuilder() Builder {
Style: fontstyle.Normal,
Color: props.NewBlack(),
},
metadata: &Metadata{},
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -241,6 +311,7 @@ func (b *builder) Build() *Config {
PageNumberPlace: b.pageNumberPlace,
Protection: b.protection,
Compression: b.compression,
Metadata: b.metadata,
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type Config struct {
PageNumberPlace props.Place
Protection *Protection
Compression bool
Metadata *Metadata
}
16 changes: 16 additions & 0 deletions pkg/config/metadata.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions pkg/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ type Provider interface {

SetProtection(protection *config.Protection)
SetCompression(compression bool)
SetMetadata(metadata *config.Metadata)
}
5 changes: 2 additions & 3 deletions pkg/maroto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 30 additions & 0 deletions pkg/providers/gofpdf/gofpdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,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)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/providers/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ 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]

Expand Down