Bank Account Statement

In this guide creating a sample bank statement document will be shown.

Before you begin

You should get your API key from your UniCloud account if you don’t have any yet.

If this is your first time using UniPDF SDK, follow this guide to setup a local development environment.

Clone the project repository

In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.

git clone https://github.com/unidoc/unipdf-examples.git

Navigate to the templates/bank-account-statement folder in the unipdf-examples directory.

cd unipdf-examples/templates/bank-account-statement

How it works

/*
* This example showcases the usage of creator templates by creating a sample
* bank account statement.
*
* Run as: go run pdf_bank_account_statement.go
*/
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"os"
"strings"
"text/template"
"time"
"github.com/unidoc/unipdf/v4/common"
"github.com/unidoc/unipdf/v4/common/license"
"github.com/unidoc/unipdf/v4/creator"
)
func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io.
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
common.SetLogger(common.NewConsoleLogger(common.LogLevelDebug))
}
func main() {
c := creator.New()
c.SetPageMargins(50, 50, 80, 25)
// Read main content template.
mainTpl, err := readTemplate("templates/main.tpl")
if err != nil {
log.Fatal(err)
}
// Read account statement data.
statement, err := readAccountStatement("account_statement.json")
if err != nil {
log.Fatal(err)
}
// Draw main content template.
tplOpts := &creator.TemplateOptions{
HelperFuncMap: template.FuncMap{
"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)
},
},
}
if err := c.DrawTemplate(mainTpl, statement, tplOpts); err != nil {
log.Fatal(err)
}
// Draw header and footer.
drawHeader := func(tplPath string, block *creator.Block, pageNum, totalPages int) {
// Read template.
tpl, err := readTemplate(tplPath)
if err != nil {
log.Fatal(err)
}
// Draw template.
data := map[string]interface{}{
"Date": time.Now(),
"Statement": statement,
"PageNum": pageNum,
"TotalPages": totalPages,
}
if err := block.DrawTemplate(c, tpl, data, tplOpts); err != nil {
log.Fatal(err)
}
}
c.DrawHeader(func(block *creator.Block, args creator.HeaderFunctionArgs) {
drawHeader("templates/header.tpl", block, args.PageNum, args.TotalPages)
})
c.DrawFooter(func(block *creator.Block, args creator.FooterFunctionArgs) {
drawHeader("templates/footer.tpl", block, args.PageNum, args.TotalPages)
})
// Write output file.
if err := c.WriteToFile("unipdf-bank-account-statement.pdf"); err != nil {
log.Fatal(err)
}
}
// readTemplate reads the template at the specified file path and returns it
// as an io.Reader.
func readTemplate(tplFile string) (io.Reader, error) {
file, err := os.Open(tplFile)
if err != nil {
return nil, err
}
defer file.Close()
buf := bytes.NewBuffer(nil)
if _, err = io.Copy(buf, file); err != nil {
return nil, err
}
return buf, nil
}
// AccountStatement represents a sample account statement.
type AccountStatement struct {
BankName string `json:"bankName"`
BankNameState string `json:"bankNameState"`
AccountNumber string `json:"accountNumber"`
DateBegin string `json:"dateBegin"`
DateEnd string `json:"dateEnd"`
CompanyName string `json:"companyName"`
CompanyAddress string `json:"companyAddress"`
ReportAddress string `json:"reportAddress"`
PhoneFree string `json:"phoneFree"`
Phone string `json:"phone"`
Tty string `json:"tTY"`
Online string `json:"online"`
White string `json:"white"`
BusinessPlanURL string `json:"businessPlanURL"`
AccountOptionsURL string `json:"accountOptionsURL"`
Advt string `json:"advt"`
BeginningBalance float64 `json:"beginningBalance"`
Withdrawals float64 `json:"withdrawals"`
Deposits float64 `json:"deposits"`
EndingBalance float64 `json:"endingBalance"`
AverageBalance float64 `json:"averageBalance"`
DepositRTN string `json:"depositRTN"`
WireRTN string `json:"wireRTN"`
StandardServiceFee float64 `json:"standardServiceFee"`
MinimumRequired float64 `json:"minimumRequired"`
ServiceFee float64 `json:"serviceFee"`
ServiceDiscount float64 `json:"serviceDiscount"`
TransactionUnits float64 `json:"transactionUnits"`
TransactionUnitsIncluded float64 `json:"transactionUnitsIncluded"`
TransactionExcessUnits float64 `json:"transactionExcessUnits"`
ServiceCharge float64 `json:"serviceCharge"`
TotalServiceCharge float64 `json:"totalServiceCharge"`
FeedbackPhone string `json:"feedbackPhone"`
TransactionDeposits float64 `json:"transactionDeposits"`
TransactionWithdrawals float64 `json:"transactionWithdrawals"`
Transactions []struct {
Date string `json:"date"`
Check interface{} `json:"check"`
Details string `json:"details"`
Deposits float64 `json:"deposits"`
Withdrawals float64 `json:"withdrawals"`
EndingDailyBalance float64 `json:"endingDailyBalance"`
} `json:"Transactions"`
AccountOptionsLabels []string `json:"accountOptionsLabels"`
}
// readAccountStatement reads the data for an account statement from a
// specified JSON file.
func readAccountStatement(jsonFile string) (*AccountStatement, error) {
file, err := os.Open(jsonFile)
if err != nil {
return nil, err
}
defer file.Close()
statement := &AccountStatement{}
if err := json.NewDecoder(file).Decode(statement); err != nil {
return nil, err
}
return statement, nil
}

UniPDF packages and other libraries are imported in the import section in lines 10-24. The init function in lines 26-35 loads the license key from environment to authenticate library request.

The main function in lines 37-125 creates the file using templates and writes the document to file. In lines 42-51 the template file and json file are read using mainTpl, err := readTemplate("templates/main.tpl") and statement, err := readAccountStatement("account_statement.json") respectively. The template options object is created in lines 54-87 using creator.TemplateOptions. In line 89 the main template is drawn using c.DrawTemplate.

In lines 94-112 a function defined for drawing header and footer elements.

In line 114-119 the footer and header are drawn by using the predefined function. Finally the pdf document is written to file in lines 122-124 using the following code.

if err := c.WriteToFile("unipdf-bank-account-statement.pdf"); err != nil {
		log.Fatal(err)
	}

The readTemplate function reads the template function and returns io.Reader buffer. In lines 145-190 AccountStatement object is defined to represent the data used for the document. The readAccountStatement function defined in lines 194-207 decodes the data in json to an AccountStatement object.

Run the code

Use the following command to run the code.

go run pdf_bank_account_statement.go

Sample Output

Preview of the output document can be shown in the image below. Bank Account Statement

Got any Questions?

We're here to help you.