You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.7 KiB
Go

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()
gen1()
//runServerInWindows()
}
func initial() {
var configFilePath string
flag.StringVar(&configFilePath, "config", "./conf/config.yaml", "配置文件")
flag.Parse()
global.Config = initialize.InitConfig(configFilePath)
global.DB = initialize.InitDB(global.Config)
global.Redis = initialize.InitRedis(global.Config)
}
/*func runServer() {
r := gin.Default()
s := &http.Server{
Addr: ":8080",
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
router.Initial(r)
err := endless.ListenAndServe(":"+global.Config.Server.Port, s.Handler)
if err != nil {
panic(err)
}
}*/
func runServerInWindows() {
r := gin.Default()
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)
}
}