Skip to content

Commit

Permalink
customizing to have more events as templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ferfabricio committed Oct 3, 2022
1 parent 2531ce1 commit a427970
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
SENDGRID_API_KEY=
SENDGRID_CERTIFICATE_TEMPLATE=
SENDGRID_CERTIFICATE_TEMPLATE=
SENDGRID_SENDER_NAME=
SENDGRID_SENDER_EMAIL=
5 changes: 3 additions & 2 deletions assets/json/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
{
"name": "Campo Mourão",
"date": "13/09/2022",
"template": "devparana_na_estrada_2022",
"attendants": [
{
"name": "Guilherme Siquineli",
"email": "guiseek@gmail.com",
"name": "Fernando Fabricio",
"email": "ferfabricio@gmail.com",
"code": "982143f3-fd81-45a6-85ef-7f6281d13235"
}
]
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {

for i := 0; i < len(c.Cities); i++ {
for j := 0; j < len(c.Cities[i].Attendants); j++ {
err = pdf.SavePDF(c.Cities[i].Name, c.Cities[i].Date, c.Cities[i].Attendants[j])
err = pdf.SavePDF(&c.Cities[i], c.Cities[i].Attendants[j])
if err != nil {
panic(err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/data/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Attendant struct {
type City struct {
Name string `json:"name"`
Date string `json:"date"`
Template string `json:"template"`
Attendants []Attendant `json:"attendants"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/email/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Send(a *data.Attendant) error {

m := mail.NewV3Mail()

from := mail.NewEmail("DevParaná", "[email protected]")
from := mail.NewEmail(os.Getenv("SENDGRID_SENDER_NAME"), os.Getenv("SENDGRID_SENDER_EMAIL"))
to := mail.NewEmail(a.Name, a.Email)

m.SetFrom(from)
Expand Down
40 changes: 4 additions & 36 deletions internal/pdf/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,21 @@ import (
"fmt"

"github.com/ferfabricio/certificados-devparana-go/internal/data"
"github.com/signintech/gopdf"
"github.com/ferfabricio/certificados-devparana-go/internal/pdf/templates"
)

// SavePDF generate and save the PDF file
func SavePDF(city string, date string, attendent data.Attendant) error {
func SavePDF(city *data.City, attendent data.Attendant) error {
var err error
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4Landscape})
pdf.AddPage()

tpl := pdf.ImportPage("./assets/pdf/template.pdf", 1, "/MediaBox")
pdf.UseImportedTemplate(tpl, 0, 0, gopdf.PageSizeA4Landscape.W, gopdf.PageSizeA4Landscape.H)

err = pdf.AddTTFFont("aleo", "./assets/fonts/Aleo-Bold.ttf")
if err != nil {
return err
}
err = pdf.AddTTFFont("opensans", "./assets/fonts/Open-Sans.ttf")
if err != nil {
return err
}

err = pdf.SetFont("aleo", "", 25)
pdf, err := templates.Prepare(attendent, city)
if err != nil {
return err
}

s, err := pdf.MeasureTextWidth(attendent.Name)
err = pdf.WritePdf(fmt.Sprintf("./output/%s.pdf", attendent.Code))
if err != nil {
return err
}

pdf.SetXY((gopdf.PageSizeA4Landscape.W/2)-(s/2), 270)

pdf.Text(attendent.Name)

pdf.SetFont("opensans", "", 13)

pdf.SetXY(350, 400)

pdf.Text(city)

pdf.SetXY(350, 420)

pdf.Text(date)

pdf.WritePdf(fmt.Sprintf("./output/%s.pdf", attendent.Code))

return nil
}
64 changes: 64 additions & 0 deletions internal/pdf/templates/devparana_na_estrada.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package templates

import (
"github.com/ferfabricio/certificados-devparana-go/internal/data"
"github.com/signintech/gopdf"
)

func DevParanaNaEstrada2022(attendent data.Attendant, city *data.City) (*gopdf.GoPdf, error) {
var err error

pdf := &gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4Landscape})
pdf.AddPage()

tpl := pdf.ImportPage("./assets/pdf/template-devparana-na-estrada-2022.pdf", 1, "/MediaBox")
pdf.UseImportedTemplate(tpl, 0, 0, gopdf.PageSizeA4Landscape.W, gopdf.PageSizeA4Landscape.H)

err = pdf.AddTTFFont("aleo", "./assets/fonts/Aleo-Bold.ttf")
if err != nil {
return nil, err
}
err = pdf.AddTTFFont("opensans", "./assets/fonts/Open-Sans.ttf")
if err != nil {
return nil, err
}

err = pdf.SetFont("aleo", "", 25)
if err != nil {
return nil, err
}

s, err := pdf.MeasureTextWidth(attendent.Name)
if err != nil {
return nil, err
}

pdf.SetXY((gopdf.PageSizeA4Landscape.W/2)-(s/2), 270)

err = pdf.Text(attendent.Name)
if err != nil {
return nil, err
}

err = pdf.SetFont("opensans", "", 13)
if err != nil {
return nil, err
}

pdf.SetXY(350, 400)

err = pdf.Text(city.Name)
if err != nil {
return nil, err
}

pdf.SetXY(350, 420)

err = pdf.Text(city.Date)
if err != nil {
return nil, err
}

return pdf, nil
}
22 changes: 22 additions & 0 deletions internal/pdf/templates/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package templates

import (
"errors"

"github.com/ferfabricio/certificados-devparana-go/internal/data"
"github.com/signintech/gopdf"
)

func getTemplate(t string, a data.Attendant, c *data.City, p *gopdf.GoPdf) (*gopdf.GoPdf, error) {
switch t {
case "devparana_na_estrada_2022":
return DevParanaNaEstrada2022(a, c)
}
return nil, errors.New("template not implemented")
}

func Prepare(a data.Attendant, c *data.City) (*gopdf.GoPdf, error) {
pdf := &gopdf.GoPdf{}

return getTemplate(c.Template, a, c, pdf)
}

0 comments on commit a427970

Please sign in to comment.