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

Cs 09(09.11) #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Binary file modified .DS_Store
Binary file not shown.
130 changes: 40 additions & 90 deletions app/certgenerator/pdf_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,72 @@ package certgenerator

import (
"bytes"
"errors"
validation "github.com/go-ozzo/ozzo-validation/v4"
"html/template"
)

type PdfData struct {
//Template string
Format string
Title string
Student string
course string
mentors string
Course string
Mentors string
Date string
}

func New() *PdfData {
return &PdfData{}
}

func (p *PdfData) SetCourse(s string) *PdfData {
p.course = s
func (p *PdfData) SetTitle(s string) *PdfData {
p.Title = s
return p
}

func (p *PdfData) SetMentors(s string) *PdfData {
p.mentors = s
func (p *PdfData) SetStudent(s string) *PdfData {
p.Student = s
return p
}

//func (p *PdfData) SetTemplate() (string, error) {
// if p.Template == "" {
// return "", errors.New("You need to pass the template data!")
// }
// return p.Template, nil
//}

func (p *PdfData) SetFormat() (string, error) {
if p.Format == "" {
return "", errors.New("You need to pass the format data!")
}
return p.Format, nil
func (p *PdfData) SetCourse(s string) *PdfData {
p.Course = s
return p
}

func (p *PdfData) SetTitle() (string, error) {
if p.Title == "" {
return "", errors.New("You need to pass the title data!")
}
return p.Title, nil
func (p *PdfData) SetMentors(s string) *PdfData {
p.Mentors = s
return p
}

func (p *PdfData) SetStudent() (string, error) {
if p.Student == "" {
return "", errors.New("You need to pass the student data!")
}
return p.Student, nil
func (p *PdfData) SetDate(s string) *PdfData {
p.Date = s
return p
}

//func (p *PdfData) SetMentors() (string, error) {
// if p.mentors == "" {
// return "", errors.New("You need to pass the mentors data!")
// }
// return p.mentors, nil
//}

func (p *PdfData) SetDate() (string, error) {
if p.Format == "" {
return "", errors.New("you need to pass the date data")
}
return p.Date, nil
func (p PdfData) Validate() error {
return validation.ValidateStruct(&p,
// Title cannot be empty
validation.Field(&p.Title, validation.Required),
// Student cannot be empty
validation.Field(&p.Student, validation.Required),
// Course cannot be empty
validation.Field(&p.Course, validation.Required),
// Mentors cannot be empty
validation.Field(&p.Mentors, validation.Required),
// Date cannot be empty
validation.Field(&p.Date, validation.Required),
)
}

func (p *PdfData) ParseTemplate(templateBytes bytes.Buffer) (bytes.Buffer, error) {
func (p *PdfData) ParseTemplate(input []byte) (*bytes.Buffer, error) {
t := template.New("certificate")
tmpl, err := t.Parse((string(input)))
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, p)
err = tmpl.Execute(buf, p)
if err != nil {
return nil, err
}
return buf, nil
}

func (p *PdfData) Validate() error {
return nil
}

//func Starter() error {
// newData := PdfData{
// Template: "sample.html",
// Format: "A4",
// Title: "Certificate Golang School",
// Student: "Khramtsov Denis",
// course: "Become a gopher",
// mentors: "Pavel Gordiyanov, Mikita Viarbovikau, Sergey Shtripling",
// Date: "08.09.2022",
// }
//
// validData, err := validator(newData)
// if err != nil {
// fmt.Println(err.Error())
// }
//
// buildData, err := build(validData)
// if err != nil {
// fmt.Println(err.Error())
// }
//
// tmpl, err := ParseTemplate(buildData)
// if err != nil {
// fmt.Println(err.Error())
// }
//
// buffer, err := pdfgenerator.GeneratePDF(tmpl)
// if err != nil {
// fmt.Println(err.Error())
// }
//
// err = Save(buffer)
// if err != nil {
// fmt.Println(err.Error())
// }
//
// fmt.Println("Done!")
//
// return nil
//}
File renamed without changes.
39 changes: 37 additions & 2 deletions app/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
package main

import (
"gus_certificates/app/certgenerator"
"fmt"
c "gus_certificates/app/certgenerator"
"gus_certificates/utils/pdfgenerator"
"io/ioutil"
"log"
)

func main() {
err := certgenerator.Starter()
data := c.New()
data.SetTitle("Certificate Golang School")
data.SetStudent("Denis Khramtsov")
data.SetCourse("Become a gopher")
data.SetMentors("Pavel Gordiyanov, Mikita Viarbovikau, Sergey Shtripling")
data.SetDate("08.09.2022")

err := data.Validate()
if err != nil {
log.Fatal(err)
}

fmt.Println("Data validate")

//Для теста считываем содержимое файла "sample.html":
template, err := ioutil.ReadFile("sample.html")
if err != nil {
log.Fatal(err)
}

buffer, err := data.ParseTemplate(template)
if err != nil {
log.Fatal(err)
}

pdf, err := pdfgenerator.GeneratePDF(buffer)
if err != nil {
log.Fatal(err)
}

//Для теста сохраняем на диске:
err = c.Save(pdf)
if err != nil {
log.Fatal(err)
}
fmt.Println("done")
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</head>
<body>
<h3>{{ .Title }}</h3>
<p>{{.Student}}</p>
<p>{{ .Student }}</p>
<table>
<thead>
<tr>
Expand Down
Binary file added data/.DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module gus_certificates

go 1.17

require github.com/SebastiaanKlippert/go-wkhtmltopdf v1.7.2
require (
github.com/SebastiaanKlippert/go-wkhtmltopdf v1.7.2
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
github.com/SebastiaanKlippert/go-wkhtmltopdf v1.7.2 h1:LORAatv6KuKheYq8HXehiwx3f/VGuzJBNSydUDQ98EM=
github.com/SebastiaanKlippert/go-wkhtmltopdf v1.7.2/go.mod h1:TY8r0gmwEL1c5Lbd66NgQCkL4ZjGDJCMVqvbbFvUx20=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand All @@ -16,12 +20,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Binary file modified utils/.DS_Store
Binary file not shown.
Binary file modified utils/storage/.DS_Store
Binary file not shown.
Binary file modified utils/storage/local/.DS_Store
Binary file not shown.
Binary file modified utils/storage/local/PDF/example.pdf
Binary file not shown.
Binary file added utils/storage/local/Templates/.DS_Store
Binary file not shown.