golang代码实例库

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3579|回复: 0

golang:使用golang生成一个签到表格pdf文件

[复制链接]

82

主题

82

帖子

486

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
486
发表于 2021-12-15 23:08:38 | 显示全部楼层 |阅读模式
实例说明
我们使用golang生成一个pdf文件,主要使用了表格的结构。
1)生成签到表
2)支持设置字体、背景色等等
3)更多例子请参看该组件example:https://github.com/tiechui1994/gopdf/tree/master/example
示意图如下:
QQ截图20211215230934.png


实例代码
[Golang] 纯文本查看 复制代码
package main

import (
        "fmt"
        "github.com/tiechui1994/gopdf"
        "github.com/tiechui1994/gopdf/core"
)

const (
        TABLE_IG = "IPAexG"
        TABLE_MD = "MPBOLD"
        TABLE_MY = "微软雅黑"
)

// CreateSignTable 创建签到表
func CreateSignTable() {
        r := core.CreateReport()
        font1 := core.FontMap{
                FontName: TABLE_IG,
                // 要将字体文件,放到程序可以访问的地方
                FileName: "E:\\gomodules\\hello\\src\\wenjiancaozuo\\SIMYOU.TTF",
        }
        font2 := core.FontMap{
                FontName: TABLE_MD,
                FileName: "E:\\gomodules\\hello\\src\\wenjiancaozuo\\SIMYOU.TTF",
        }
        font3 := core.FontMap{
                FontName: TABLE_MY,
                FileName: "E:\\gomodules\\hello\\src\\wenjiancaozuo\\SIMYOU.TTF",
        }
        r.SetFonts([]*core.FontMap{&font1, &font2, &font3})
        r.SetPage("A4", "P")

        // 一定要设置字体,否则执行执行r.Cell会报错
        r.SetFont(TABLE_IG, 18)
        // 这里坐标是不断调整后的结果
        r.Cell(240, 50, "培训签到表")

        // 注册个表格生成器
        r.RegisterExecutor(core.Executor(SimpleTableExecutor), core.Detail)

        r.Execute("sign.pdf")
        fmt.Println(r.GetCurrentPageNo())
}

func SimpleTableExecutor(report *core.Report) {
        lineSpace := 0.0
        // 每行文字的间距
        lineHeight := 18.0

        // 创建一个表格,4列,30行,宽度600
        table := gopdf.NewTable(4, 30, 600, lineHeight, report)
        table.SetMargin(core.Scope{})

        // 合并单元格
        c00 := table.NewCellByRange(4, 1) // 第1行,4个单元格合并
        c10 := table.NewCellByRange(2, 1) // 第2行,2个单元格合并
        c11 := table.NewCellByRange(2, 1) // 第2行,又一个2个单元格合并

        f1 := core.Font{Family: TABLE_MY, Size: 15, Style: ""}
        // 这里设置的是字内容和边框的距离
        border := core.NewScope(4.0, 4.0, 4.0, 4.0)
        // table.GetColWidth中, (0,0)第1行,第1列,(1,0),第2行,第1列,(1,2),第2行,第3列,虽然第2行,合并成只有两个单元格,但获取长度还是按原来默认表格计数
        // 这里可以设置背景色,字体对齐样式等。
        c00.SetElement(gopdf.NewTextCell(table.GetColWidth(0, 0), lineHeight, lineSpace, report).SetFont(f1).SetBorder(border).HorizontalCentered().SetContent("注意:不可以代替他人签到"))
        c10.SetElement(gopdf.NewTextCell(table.GetColWidth(1, 0), lineHeight, lineSpace, report).SetFont(f1).SetBorder(border).SetBackColor("205,170,125").VerticalCentered().SetContent("课程:学习go语言"))
        c11.SetElement(gopdf.NewTextCell(table.GetColWidth(1, 2), lineHeight, lineSpace, report).SetFont(f1).SetBorder(border).SetBackColor("205,170,125").VerticalCentered().SetContent("网址:[url=http://www.golangcodes.com]www.golangcodes.com[/url]"))
        f1 = core.Font{Family: TABLE_MY, Size: 10}
        border = core.NewScope(4.0, 4.0, 0, 0)

        for i := 0; i < 28; i++ {
                cells := make([]*gopdf.TableCell, 4)
                for j := 0; j < 4; j++ {
                        cells[j] = table.NewCell()
                }

                for j := 0; j < 4; j++ {
                        w := table.GetColWidth(i+2, j)
                        e := gopdf.NewTextCell(w, lineHeight, lineSpace, report)
                        e.SetFont(f1)
                        if i%2 == 0 {
                                if i == 0 {
                                        e.SetBackColor("193,205,205")

                                } else {
                                        e.SetBackColor("224,238,238")
                                }
                        }
                        e.SetBorder(border)
                        content := ""
                        if i == 0 && j == 0 {
                                content = "姓名"
                        } else if i == 0 && j == 1 {
                                content = "上午"
                        } else if i == 0 && j == 2 {
                                content = "下午"
                        } else if i == 0 && j == 3 {
                                content = "晚上"
                        }
                        e.SetContent(content)
                        cells[j].SetElement(e)
                }
        }

        table.GenerateAtomicCell()
}

func main() {
        CreateSignTable()
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|golang代码实例库 ( 粤ICP备2021162396号 )

GMT+8, 2025-1-22 11:21 , Processed in 0.026887 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表