pdf.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/jung-kurt/gofpdf"
  5. )
  6. // 官方测试实例 https://github.com/jung-kurt/gofpdf/blob/master/fpdf_test.go
  7. func main() {
  8. pdf := gofpdf.New("P", "mm", "A4", "")
  9. //增加一页
  10. pdf.AddPage()
  11. pdf.SetFont("Arial", "B", 16) // 设置字体 加粗 字号
  12. // 表格显示样式设置
  13. // width, height, text, border, position after, align, fill, link, linkStr
  14. pdf.CellFormat(0, 0, "Hello, world", "0", 0, "LM", false, 0, "")
  15. // 设置超链接
  16. //获取 line height
  17. _, lineHt := pdf.GetFontSize()
  18. link := pdf.AddLink()
  19. pdf.WriteLinkID(lineHt+20, "here", link)
  20. pdf.SetFont("", "", 0)
  21. pdf.SetLink(link, 0, -1)
  22. pdf.Image("./pdf/222.jpg", 10, 12, 30, 0, false, "", 0, "http://baidu.com")
  23. //设置页眉
  24. pdf.SetHeaderFuncMode(func() {
  25. pdf.Image("./pdf/111.jpeg", 5, 5, 100, 5, false, "", 0, "")
  26. pdf.SetY(10)
  27. pdf.Ln(10)
  28. }, true)
  29. //设置页脚
  30. pdf.SetFooterFunc(func() {
  31. pdf.SetY(-10)
  32. pdf.CellFormat(
  33. 0, 10,
  34. fmt.Sprintf("-%d-", pdf.PageNo()), //字符串中的 {nb}。大括号是可以省的,但不建议这么做
  35. "", 0, "C", false, 0, "",
  36. )
  37. })
  38. pdf.AddPage()
  39. // 增加图片
  40. pdf.ImageOptions(
  41. "./pdf/222.jpg",
  42. 10, 30,
  43. 150, 100,
  44. false,
  45. gofpdf.ImageOptions{ImageType: "jpg", ReadDpi: true}, //ImageType:只支持:JPG、JPEG、PNG、GIF,这个四个格式。无所谓字符串大小写。如果为空,则根据文件扩展名自行推导
  46. 0,
  47. "",
  48. )
  49. pdf.AddPage()
  50. err := pdf.OutputFileAndClose("hello.pdf")
  51. if err != nil {
  52. fmt.Println(err.Error())
  53. }
  54. }