Skip to content

Commit

Permalink
[US-20] Update examples with new builtin helper functions (#249)
Browse files Browse the repository at this point in the history
* updated examples with new builtin helper functions

* updated examples

* uncomment logger
  • Loading branch information
kellemNegasi committed May 2, 2024
1 parent 69dfa68 commit af296ca
Show file tree
Hide file tree
Showing 11 changed files with 8 additions and 130 deletions.
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

0 comments on commit af296ca

Please sign in to comment.