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

[US-20] Update examples with new builtin helper functions #249

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736
github.com/unidoc/pkcs7 v0.2.0
github.com/unidoc/unichart v0.3.0
github.com/unidoc/unipdf/v3 v3.57.0
github.com/unidoc/unipdf/v3 v3.58.0
github.com/wcharczuk/go-chart/v2 v2.1.0
golang.org/x/crypto v0.22.0
golang.org/x/image v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a h1:RLtvUhe4DsUDl6
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a/go.mod h1:j+qMWZVpZFTvDey3zxUkSgPJZEX33tDgU/QIA0IzCUw=
github.com/unidoc/unichart v0.3.0 h1:VX1j5yzhjrR3f2flC03Yat6/WF3h7Z+DLEvJLoTGhoc=
github.com/unidoc/unichart v0.3.0/go.mod h1:8JnLNKSOl8yQt1jXewNgYFHhFm5M6/ZiaydncFDpakA=
github.com/unidoc/unipdf/v3 v3.57.0 h1:C6t0MMC5MI336gqapHckRg+szYce1EomPjTlE/XHjQA=
github.com/unidoc/unipdf/v3 v3.57.0/go.mod h1:HEGsUAyg0cI46ofB2D4b6FzBXzVM2P1mHvQ5R+HxONs=
github.com/unidoc/unipdf/v3 v3.58.0 h1:c2yWEw1FLxwoVCjcuUTeOAQn/HIHsh+zq+wlVFGwgKc=
github.com/unidoc/unipdf/v3 v3.58.0/go.mod h1:HEGsUAyg0cI46ofB2D4b6FzBXzVM2P1mHvQ5R+HxONs=
github.com/unidoc/unitype v0.4.0 h1:/TMZ3wgwfWWX64mU5x2O9no9UmoBqYCB089LYYqHyQQ=
github.com/unidoc/unitype v0.4.0/go.mod h1:HV5zuUeqMKA4QgYQq3KDlJY/P96XF90BQB+6czK6LVA=
github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I=
Expand Down
18 changes: 0 additions & 18 deletions templates/airplane-ticket/pdf_airplane_ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/boombuler/barcode/qr"
"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/core"
"github.com/unidoc/unipdf/v3/creator"
"github.com/unidoc/unipdf/v3/model"
)
Expand Down Expand Up @@ -68,23 +67,6 @@ func main() {
t, _ := time.Parse("2006-01-02T15:04:05", val)
return t.Format(format)
},
"extendDict": func(m map[string]interface{}, params ...interface{}) (map[string]interface{}, error) {
lenParams := len(params)
if lenParams%2 != 0 {
return nil, core.ErrRangeError
}

for i := 0; i < lenParams; i += 2 {
key, ok := params[i].(string)
if !ok {
return nil, core.ErrTypeError
}

m[key] = params[i+1]
}

return m, nil
},
},
}

Expand Down
3 changes: 0 additions & 3 deletions templates/aviation-checklist/pdf_aviation_checklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ func main() {
"isFitInPageHeight": func(height float64) bool {
return height <= c.Height()-320
},
"add": func(a, b float64) float64 {
return a + b
},
},
}

Expand Down
31 changes: 4 additions & 27 deletions templates/bank-account-statement/pdf_bank_account_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/core"
"github.com/unidoc/unipdf/v3/creator"
)

Expand Down Expand Up @@ -53,36 +52,14 @@ func main() {
// Draw main content template.
tplOpts := &creator.TemplateOptions{
HelperFuncMap: template.FuncMap{
"formatTime": func(val, format string) string {
t, _ := time.Parse("2006-01-02T15:04:05", val)
return t.Format(format)
},
"extendDict": func(m map[string]interface{}, params ...interface{}) (map[string]interface{}, error) {
lenParams := len(params)
if lenParams%2 != 0 {
return nil, core.ErrRangeError
}

out := make(map[string]interface{}, len(m))
for key, val := range m {
out[key] = val
}

for i := 0; i < lenParams; i += 2 {
key, ok := params[i].(string)
if !ok {
return nil, core.ErrTypeError
}

out[key] = params[i+1]
}

return out, nil
},
"strRepeat": strings.Repeat,
"loop": func(size uint64) []struct{} {
return make([]struct{}, size)
},
"formatTime": func(val, format string) string {
t, _ := time.Parse("2006-01-02T15:04:05", val)
return t.Format(format)
},
},
}

Expand Down
21 changes: 0 additions & 21 deletions templates/boarding-pass/pdf_boarding_pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import (
"io"
"log"
"os"
"text/template"

"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/core"
"github.com/unidoc/unipdf/v3/creator"
"github.com/unidoc/unipdf/v3/model"
)
Expand Down Expand Up @@ -62,25 +60,6 @@ func main() {
ImageMap: map[string]*model.Image{
"qr-code-1": qrCode,
},
HelperFuncMap: template.FuncMap{
"extendDict": func(m map[string]interface{}, params ...interface{}) (map[string]interface{}, error) {
lenParams := len(params)
if lenParams%2 != 0 {
return nil, core.ErrRangeError
}

for i := 0; i < lenParams; i += 2 {
key, ok := params[i].(string)
if !ok {
return nil, core.ErrTypeError
}

m[key] = params[i+1]
}

return m, nil
},
},
}

if err := c.DrawTemplate(mainTpl, pass, tplOpts); err != nil {
Expand Down
3 changes: 0 additions & 3 deletions templates/documentation/pdf_templates_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,6 @@ func createTplOpts(c *creator.Creator) *creator.TemplateOptions {

// Create function map.
funcMap := template.FuncMap{
"array": func(elements ...interface{}) []interface{} {
return elements
},
"loop": func(size uint64) []struct{} {
return make([]struct{}, size)
},
Expand Down
3 changes: 0 additions & 3 deletions templates/log-book-report/pdf_log_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ func main() {
"isEven": func(num int) bool {
return num%2 == 0
},
"add": func(num1, num2 int) int {
return num1 + num2
},
"getSlice": func(s string) []string {
return strings.Split(s, ",")
},
Expand Down
27 changes: 1 addition & 26 deletions templates/medical-bill/pdf_medical_bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import (
"io"
"log"
"os"
"text/template"

"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/core"
"github.com/unidoc/unipdf/v3/creator"
)

Expand Down Expand Up @@ -48,29 +46,6 @@ func main() {
log.Fatal(err)
}

// Draw main content template.
tplOpts := &creator.TemplateOptions{
HelperFuncMap: template.FuncMap{
"extendDict": func(m map[string]interface{}, params ...interface{}) (map[string]interface{}, error) {
lenParams := len(params)
if lenParams%2 != 0 {
return nil, core.ErrRangeError
}

for i := 0; i < lenParams; i += 2 {
key, ok := params[i].(string)
if !ok {
return nil, core.ErrTypeError
}

m[key] = params[i+1]
}

return m, nil
},
},
}

data := map[string]interface{}{
"institution": map[string]interface{}{
"name": "UniDoc Medial Center",
Expand All @@ -80,7 +55,7 @@ func main() {
"bill": bill,
}

if err := c.DrawTemplate(mainTpl, data, tplOpts); err != nil {
if err := c.DrawTemplate(mainTpl, data, nil); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion templates/receipt/pdf_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
c := creator.New()
c.SetPageMargins(15, 15, 20, 20)
c.SetPageSize(creator.PageSizeA5)

// Read main content template.
tpl, err := readTemplate("./templates/main.tpl")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion templates/rental-agreement/pdf_rental_agreement.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func main() {
},
HelperFuncMap: template.FuncMap{
"formatTime": func(val, format string) string {
t, _ := time.Parse("2006-01-02T00:00:00", val)
t, _ := time.Parse("2006-01-02T15:04:05Z", val)
return t.Format(format)
},
"listItems": func(items []string, useAnd bool) string {
Expand Down
25 changes: 1 addition & 24 deletions templates/trade-confirmation/pdf_trade_confirmation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import (
"io"
"log"
"os"
"text/template"

"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/core"
"github.com/unidoc/unipdf/v3/creator"
)

Expand Down Expand Up @@ -49,35 +47,14 @@ func main() {
}

// Draw main content template.
tplOpts := &creator.TemplateOptions{
HelperFuncMap: template.FuncMap{
"extendDict": func(m map[string]interface{}, params ...interface{}) (map[string]interface{}, error) {
lenParams := len(params)
if lenParams%2 != 0 {
return nil, core.ErrRangeError
}

for i := 0; i < lenParams; i += 2 {
key, ok := params[i].(string)
if !ok {
return nil, core.ErrTypeError
}

m[key] = params[i+1]
}

return m, nil
},
},
}

data := map[string]interface{}{
"firmName": "UniDoc Financial Firm",
"firmAddress": "123 Main Street\nPortland, ME 12345\n(123) 456-789",
"trade": trade,
}

if err := c.DrawTemplate(mainTpl, data, tplOpts); err != nil {
if err := c.DrawTemplate(mainTpl, data, nil); err != nil {
log.Fatal(err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ func main() {
tplOpts := &creator.TemplateOptions{
ImageMap: imageMap,
HelperFuncMap: template.FuncMap{
"add": func(a, b int) int {
return a + b
},
"createBarcode": func(text string) (string, error) {
barcode, err := code128.Encode(text)
if err != nil {
Expand Down