johnfercher/maroto 借鑒了bootstrap 的網格模式,使用了gofpdf 生成pdf,是一個很不錯的golang pdf 工具
有一個問題是不支持中文(因為配置寫的的原因)看到網上有一個中國人fork添加了AddUTF8Font 支持,這樣
中文就可以顯示了,以下是一個參考的使用
參考特性
核心代碼
package main
import (
"fmt"
"os"
"time"
"github.com/Vale-sail/maroto/pkg/consts"
"github.com/Vale-sail/maroto/pkg/pdf"
"github.com/Vale-sail/maroto/pkg/props"
)
func main() {
begin := time.Now()
m := pdf.NewMarotoCustomSize(consts.Landscape, "C6", "mm", 114.0, 162.0)
m.SetPageMargins(5, 5, 5)
m.AddUTF8Font("NotoSansSC", "", "./font/NotoSansSC-Regular.ttf")
m.AddUTF8Font("NotoSansSC", "I", "./font/NotoSansSC-Regular.ttf")
m.AddUTF8Font("NotoSansSC", "B", "./font/NotoSansSC-Regular.ttf")
m.AddUTF8Font("NotoSansSC", "BI", "./font/NotoSansSC-Regular.ttf")
// m.SetBorder(true)
m.Row(40, func() {
m.Col(4, func() {
_ = m.FileImage("biplane.jpg", props.Rect{
Center: true,
Percent: 50,
})
})
m.Col(4, func() {
m.Text("Gopher International Shipping, Inc.", props.Text{
Top: 12,
Size: 12,
Extrapolate: true,
})
})
m.ColSpace(4)
})
m.Line(10)
m.Row(30, func() {
m.Col(12, func() {
m.Text("João Sant'Ana 100 Main Street", props.Text{
Size: 10,
Align: consts.Right,
})
m.Text("榮鋒亮 TN 39021", props.Text{
Size: 10,
Align: consts.Right,
Family: "NotoSansSC",
Top: 10,
})
m.Text("United States (USA)", props.Text{
Size: 10,
Align: consts.Right,
Top: 20,
})
})
})
m.Row(30, func() {
m.Col(12, func() {
m.QrCode("https://cnblogs.com/rongfengliang")
})
})
err := m.OutputFileAndClose("customsize.pdf")
if err != nil {
fmt.Println("Could not save PDF:", err)
os.Exit(1)
}
end := time.Now()
fmt.Println(end.Sub(begin))
}
說明
實際上同時也應該保留AddUTF8FontFromBytes,同時也應該添加中文編碼的常量,可以方便軟件的分發以及使用(后邊fork修改一個版本)
參考資料
https://github.com/johnfercher/maroto
https://github.com/Vale-sail/maroto