forked from tiechui1994/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex_report_test.go
215 lines (188 loc) · 7.53 KB
/
complex_report_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package example
import (
"fmt"
"path/filepath"
"testing"
"time"
"github.com/tiechui1994/gopdf"
"github.com/tiechui1994/gopdf/core"
)
const (
ErrFile = 1
FONT_MY = "微软雅黑"
FONT_MD = "MPBOLD"
DateFormat = "2006-01-02 15:04:05"
)
var (
largeFont = core.Font{Family: FONT_MY, Size: 15}
headFont = core.Font{Family: FONT_MY, Size: 12}
textFont = core.Font{Family: FONT_MY, Size: 10}
)
func ComplexReport() {
r := core.CreateReport()
font1 := core.FontMap{
FontName: FONT_MY,
FileName: "ttf//microsoft.ttf",
}
font2 := core.FontMap{
FontName: FONT_MD,
FileName: "ttf//mplus-1p-bold.ttf",
}
r.SetFonts([]*core.FontMap{&font1, &font2})
r.SetPage("A4", "P")
r.FisrtPageNeedHeader = true
r.FisrtPageNeedFooter = true
r.RegisterExecutor(core.Executor(ComplexReportExecutor), core.Detail)
r.RegisterExecutor(core.Executor(ComplexReportFooterExecutor), core.Footer)
r.RegisterExecutor(core.Executor(ComplexReportHeaderExecutor), core.Header)
r.Execute(fmt.Sprintf("complex_report_test.pdf"))
r.SaveAtomicCellText("complex_report_test.txt")
}
func ComplexReportExecutor(report *core.Report) {
var (
data ReportDetail
lineSpace = 1.0
lineHight = 16.0
)
ret, errStr := getReportDetailData(&data)
if ret != 0 {
panic(struct {
ret int
errStr string
}{ret: ret, errStr: errStr})
}
dir, _ := filepath.Abs("pictures")
qrcodeFile := fmt.Sprintf("%v/qrcode.png", dir)
line := gopdf.NewHLine(report).SetMargin(core.Scope{Top: 3.0, Bottom: 6.8}).SetWidth(0.1)
// todo: 任务详情
div := gopdf.NewDivWithWidth(100, lineHight, lineSpace, report)
div.SetFont(largeFont)
div.SetContent("测试报告").GenerateAtomicCell()
line.GenerateAtomicCell()
// 二维码
im := gopdf.NewImageWithWidthAndHeight(qrcodeFile, 70, 70, report)
im.SetMargin(core.Scope{Left: 340, Top: -40})
im.GenerateAtomicCell()
// 基本信息
report.SetMargin(10, -25)
baseInfoDiv := gopdf.NewDivWithWidth(100, lineHight, lineSpace, report)
baseInfoDiv.SetFont(headFont)
baseInfoDiv.SetContent("报告概要").GenerateAtomicCell()
baseInfo := gopdf.NewDivWithWidth(200, lineHight, lineSpace, report)
baseInfo.SetMarign(core.Scope{Left: 12, Top: 3.0})
baseInfo.SetFont(textFont).SetContent(fmt.Sprintf("任务: %s", data.JobName)).GenerateAtomicCell()
baseInfo.Copy(fmt.Sprintf("创建人: %s", data.CreatUserName)).GenerateAtomicCell()
baseInfo.Copy(fmt.Sprintf("状态: %s", data.Status)).GenerateAtomicCell()
baseInfo.Copy(fmt.Sprintf("类别: %s", data.IssueClassName)).GenerateAtomicCell()
// 模板
report.SetMargin(6, 3)
baseInfoDiv.Copy("详细过程").GenerateAtomicCell()
report.SetMargin(0, 3)
SimpleTableReportExecutor(report)
// todo: 评论
report.SetMargin(0, 10)
div.Copy("评论信息").GenerateAtomicCell()
line.GenerateAtomicCell()
if len(data.Contents) == 0 {
nodataDiv := gopdf.NewDivWithWidth(150, lineHight, lineSpace, report)
nodataDiv.SetFont(textFont).SetContent("\t没有回复记录").GenerateAtomicCell()
report.SetMargin(0, 3)
}
for _, content := range data.Contents {
cellStr := fmt.Sprintf("\t%s %s %s", content.Time, content.Msg, content.CreateUser)
comment := gopdf.NewDivWithWidth(415, lineHight, lineSpace, report)
comment.SetFont(textFont).SetContent(cellStr).GenerateAtomicCell()
report.SetMargin(0, 3)
}
// todo: 历史记录
report.SetMargin(0, 3)
historyDiv := div.Copy("历史回复")
historyDiv.GenerateAtomicCell()
line.GenerateAtomicCell()
if len(data.History) == 0 {
nodataDiv := gopdf.NewDivWithWidth(150, lineHight, lineSpace, report)
nodataDiv.SetFont(textFont).SetContent("\t没有历史记录").GenerateAtomicCell()
report.SetMargin(0, 3)
}
for _, content := range data.History {
cellStr := fmt.Sprintf("\t%s %s %s", content.Time, content.Msg, content.CreateUser)
comment := gopdf.NewDivWithWidth(415, lineHight, lineSpace, report)
comment.SetFont(textFont).SetContent(cellStr).GenerateAtomicCell()
report.SetMargin(0, 3)
}
}
func ComplexReportFooterExecutor(report *core.Report) {
content := fmt.Sprintf("第 %v / {#TotalPage#} 页", report.GetCurrentPageNo())
footer := gopdf.NewSpan(10, 0, report)
footer.SetFont(textFont)
footer.SetFontColor("60, 179, 113")
footer.HorizontalCentered().SetContent(content).GenerateAtomicCell()
}
func ComplexReportHeaderExecutor(report *core.Report) {
content := "github.com/tiechui1994/gopdf"
footer := gopdf.NewSpan(10, 0, report)
footer.SetFont(textFont)
footer.SetFontColor("255,0,0")
footer.SetBorder(core.Scope{Top: 10})
footer.HorizontalCentered().SetContent(content).GenerateAtomicCell()
}
type ExportInfo struct {
Time string
Msg string
CreateUser string
}
type ReportDetail struct {
JobName string
CreatedAt string
CreatUserName string
Status string
IssueClassName string
IssueSubClassName string
TimeOut string
Template map[string]string
Contents []ExportInfo
History []ExportInfo
}
func getReportDetailData(data *ReportDetail) (ret int, errStr string) {
data.JobName = "技术指导"
data.CreatedAt = time.Now().Format(DateFormat)
data.CreatUserName = "钱伟长"
data.Status = "已经完成"
data.IssueClassName = "发动机类别"
data.IssueSubClassName = "测试飞机发动机"
data.Contents = []ExportInfo{
{
time.Now().Format("2006-01-02 15:03:04"),
"涡扇发动机结构,涡扇发动机通俗的讲可以看做两根粗细不同的管子套在一起组成的。细管子里包含了低高压气机、燃烧室、低高压涡轮,最后连接至尾喷管,这根内管所包裹的空间叫做发动机的内涵道,流经里头的空气叫内涵气流。而套在内涵道外面的粗管子则包裹着风扇以及整个或者部分细管子(内涵道),我们需要注意。",
"钱学森",
},
{
time.Now().Format("2006-01-02 15:03:04"),
"涡扇发动机工作原理,发动机前方的风扇旋转吸入空气被分为两个部分,一部分进入细管子成为内涵气流,一部分进入粗管子成为外涵气流,外涵气流直接从发动机尾部流出形成一部分动力,而内涵气流经过压气机被压缩,成为高温高压气体,并进一步进入燃烧室被和燃油一起进一步加热膨胀冲击后面的涡轮,涡轮就像我们小时候玩的纸风车一样,被高温高压燃气带动旋转,燃气最后从尾部高速喷出,形成发动机最主要的动力。",
"冯卡门",
},
{
time.Now().Format("2006-01-02 15:03:04"),
"由于涡轮与风扇、压气机同在一根轴承,因此涡轮又带动了风扇、压气机一起转动。吸入空气—增压-加热-喷射—带动发动机运转,这样一个稳定的循环运动就初步建立起来,是不是看起来很像一个永动机,只要发动机一发动,就能持续的提供动力。",
"爱因斯坦",
},
}
data.History = []ExportInfo{
{
time.Now().Format("2006-01-02 15:03:04"),
"涡扇发动机启动,其实现代的飞机尾部一般会加装了一个辅助动力装置,叫做APU.它是涡扇发动机能够启动的关键所在,APU是由一个小电动机带动的小型" +
"燃气轮机,在APU启动后,就能源源不断的吸入空气并送到发动机后面的燃烧室燃烧然后带动整个涡扇发动机的运转,所以涡扇发动机启动前,必须先启动APU,如果" +
"APU故障了就只能依靠地面电源车和高压气源车来实现发动机的启动",
"邓稼先",
},
{
time.Now().Format("2006-01-02 15:03:04"),
"很好, 照办",
"毛泽东",
},
}
return ret, errStr
}
func TestJobExport(t *testing.T) {
ComplexReport()
}