diff --git a/cmd/web/main.go b/cmd/web/main.go index ab4f7ed..36c7fa1 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -2,17 +2,21 @@ package main import ( "flag" + "fmt" "github.com/gin-gonic/gin" + "github.com/jung-kurt/gofpdf" "insure/global" "insure/initialize" "insure/router" + "strconv" ) func main() { initial() //testVerify() //runServer() - runServerInWindows() + gen1() + //runServerInWindows() } func initial() { @@ -45,3 +49,149 @@ func runServerInWindows() { router.Initial(r) r.Run(":" + global.Config.Server.Port) } + +func gen() { + pdf := gofpdf.New("P", "mm", "A4", "") // 210mm x 297mm + pdf.AddPage() + + for fontSize := 4; fontSize < 40; fontSize += 10 { + pdf.SetFont("Arial", "S", float64(fontSize)) + pdf.SetXY(0, float64(fontSize)) + pdf.Cell(40, 10, "Hello World") + } + err := pdf.OutputFileAndClose("./test.pdf") + if err != nil { + fmt.Println(err) + } +} + +func strDelimit(str string, sepstr string, sepcount int) string { + pos := len(str) - sepcount + for pos > 0 { + str = str[:pos] + sepstr + str[pos:] + pos = pos - sepcount + } + return str +} + +func gen1() { + pdf := gofpdf.New("P", "mm", "A4", "") + + type countryType struct { + nameStr, capitalStr, areaStr, popStr string + } + countryList := make([]countryType, 0, 8) + header := []string{"Country", "Capital", "Area (sq km)", "Pop. (thousands)"} + for i := 0; i < 8; i++ { + countryList = append(countryList, countryType{ + nameStr: "name" + strconv.Itoa(i), + capitalStr: "capital" + strconv.Itoa(i), + areaStr: "area" + strconv.Itoa(i), + popStr: "pop" + strconv.Itoa(i), + }) + } + // Simple table + basicTable := func() { + left := (210.0 - 4*40) / 2 + pdf.SetX(left) + for _, str := range header { + pdf.CellFormat(40, 7, str, "1", 0, "", false, 0, "") + } + pdf.Ln(-1) + for _, c := range countryList { + pdf.SetX(left) + pdf.CellFormat(40, 6, c.nameStr, "1", 0, "", false, 0, "") + pdf.CellFormat(40, 6, c.capitalStr, "1", 0, "", false, 0, "") + pdf.CellFormat(40, 6, c.areaStr, "1", 0, "", false, 0, "") + pdf.CellFormat(40, 6, c.popStr, "1", 0, "", false, 0, "") + pdf.Ln(-1) + } + } + // Better table + improvedTable := func() { + // Column widths + w := []float64{40.0, 35.0, 40.0, 45.0} + wSum := 0.0 + for _, v := range w { + wSum += v + } + left := (210 - wSum) / 2 + // Header + pdf.SetX(left) + for j, str := range header { + pdf.CellFormat(w[j], 7, str, "1", 0, "C", false, 0, "") + } + pdf.Ln(-1) + // Data + for _, c := range countryList { + pdf.SetX(left) + pdf.CellFormat(w[0], 6, c.nameStr, "LR", 0, "", false, 0, "") + pdf.CellFormat(w[1], 6, c.capitalStr, "LR", 0, "", false, 0, "") + pdf.CellFormat(w[2], 6, strDelimit(c.areaStr, ",", 3), + "LR", 0, "R", false, 0, "") + pdf.CellFormat(w[3], 6, strDelimit(c.popStr, ",", 3), + "LR", 0, "R", false, 0, "") + pdf.Ln(-1) + } + pdf.SetX(left) + pdf.CellFormat(wSum, 0, "", "T", 0, "", false, 0, "") + } + // Colored table + fancyTable := func() { + // Colors, line width and bold font + pdf.SetFillColor(255, 0, 0) + pdf.SetTextColor(255, 255, 255) + pdf.SetDrawColor(128, 0, 0) + pdf.SetLineWidth(.3) + pdf.SetFont("", "B", 0) + // Header + w := []float64{40, 35, 40, 45} + wSum := 0.0 + for _, v := range w { + wSum += v + } + left := (210 - wSum) / 2 + pdf.SetX(left) + for j, str := range header { + pdf.CellFormat(w[j], 7, str, "1", 0, "C", true, 0, "") + } + pdf.Ln(-1) + // Color and font restoration + pdf.SetFillColor(224, 235, 255) + pdf.SetTextColor(0, 0, 0) + pdf.SetFont("", "", 0) + // Data + fill := false + for _, c := range countryList { + pdf.SetX(left) + pdf.CellFormat(w[0], 6, c.nameStr, "LR", 0, "", fill, 0, "") + pdf.CellFormat(w[1], 6, c.capitalStr, "LR", 0, "", fill, 0, "") + pdf.CellFormat(w[2], 6, strDelimit(c.areaStr, ",", 3), + "LR", 0, "R", fill, 0, "") + pdf.CellFormat(w[3], 6, strDelimit(c.popStr, ",", 3), + "LR", 0, "R", fill, 0, "") + pdf.Ln(-1) + fill = !fill + } + pdf.SetX(left) + pdf.CellFormat(wSum, 0, "", "T", 0, "", false, 0, "") + } + + pdf.AddPage() + + pdf.SetFont("宋体", "", float64(40)) + pdf.SetXY(0, float64(40)) + pdf.Cell(40, 10, "注意:请认准阅读所附条款,/n尤其是黑体部分内容") + + pdf.SetFont("Arial", "", 14) + + basicTable() + pdf.AddPage() + improvedTable() + pdf.AddPage() + fancyTable() + err := pdf.OutputFileAndClose("./test2.pdf") + if err != nil { + fmt.Println(err) + } +} diff --git a/go.mod b/go.mod index 12bb6f6..35bfcab 100644 --- a/go.mod +++ b/go.mod @@ -32,6 +32,7 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/jung-kurt/gofpdf v1.16.2 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/kr/text v0.2.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect diff --git a/go.sum b/go.sum index c74443c..52a6ad0 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= @@ -72,6 +73,9 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= +github.com/jung-kurt/gofpdf v1.16.2 h1:jgbatWHfRlPYiK85qgevsZTHviWXKwB1TTiKdz5PtRc= +github.com/jung-kurt/gofpdf v1.16.2/go.mod h1:1hl7y57EsiPAkLbOwzpzqgx1A30nQCk/YmFV8S2vmK0= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= @@ -93,6 +97,8 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/phpdave11/gofpdi v1.0.7/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= @@ -105,10 +111,12 @@ github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -138,6 +146,7 @@ golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/insure b/insure index 7398476..87f6390 100755 Binary files a/insure and b/insure differ diff --git a/test.pdf b/test.pdf new file mode 100644 index 0000000..64eb80b Binary files /dev/null and b/test.pdf differ diff --git a/test2.pdf b/test2.pdf new file mode 100644 index 0000000..586708b Binary files /dev/null and b/test2.pdf differ