package main import ( "fmt" "github.com/jung-kurt/gofpdf" ) // 官方测试实例 https://github.com/jung-kurt/gofpdf/blob/master/fpdf_test.go func main() { pdf := gofpdf.New("P", "mm", "A4", "") //增加一页 pdf.AddPage() pdf.SetFont("Arial", "B", 16) // 设置字体 加粗 字号 // 表格显示样式设置 // width, height, text, border, position after, align, fill, link, linkStr pdf.CellFormat(0, 0, "Hello, world", "0", 0, "LM", false, 0, "") // 设置超链接 //获取 line height _, lineHt := pdf.GetFontSize() link := pdf.AddLink() pdf.WriteLinkID(lineHt+20, "here", link) pdf.SetFont("", "", 0) pdf.SetLink(link, 0, -1) pdf.Image("./pdf/222.jpg", 10, 12, 30, 0, false, "", 0, "http://baidu.com") //设置页眉 pdf.SetHeaderFuncMode(func() { pdf.Image("./pdf/111.jpeg", 5, 5, 100, 5, false, "", 0, "") pdf.SetY(10) pdf.Ln(10) }, true) //设置页脚 pdf.SetFooterFunc(func() { pdf.SetY(-10) pdf.CellFormat( 0, 10, fmt.Sprintf("-%d-", pdf.PageNo()), //字符串中的 {nb}。大括号是可以省的,但不建议这么做 "", 0, "C", false, 0, "", ) }) pdf.AddPage() // 增加图片 pdf.ImageOptions( "./pdf/222.jpg", 10, 30, 150, 100, false, gofpdf.ImageOptions{ImageType: "jpg", ReadDpi: true}, //ImageType:只支持:JPG、JPEG、PNG、GIF,这个四个格式。无所谓字符串大小写。如果为空,则根据文件扩展名自行推导 0, "", ) pdf.AddPage() err := pdf.OutputFileAndClose("hello.pdf") if err != nil { fmt.Println(err.Error()) } }