1. 生成PDF

一个简单但是非常实用的pdf生成器!

安装:

  1. go get github.com/jung-kurt/gofpdf

代码:

  1. package main
  2. import (
  3. "github.com/jung-kurt/gofpdf"
  4. )
  5. func main() {
  6. err := GeneratePdf("hello.pdf")
  7. if err != nil {
  8. panic(err)
  9. }
  10. }
  11. // GeneratePdf generates our pdf by adding text and images to the page
  12. // then saving it to a file (name specified in params).
  13. func GeneratePdf(filename string) error {
  14. pdf := gofpdf.New("P", "mm", "A4", "")
  15. pdf.AddPage()
  16. pdf.SetFont("Arial", "B", 16)
  17. // CellFormat(width, height, text, border, position after, align, fill, link, linkStr)
  18. pdf.CellFormat(190, 7, "Welcome to topgoer.com", "0", 0, "CM", false, 0, "")
  19. // ImageOptions(src, x, y, width, height, flow, options, link, linkStr)
  20. pdf.ImageOptions(
  21. "topgoer.png",
  22. 80, 20,
  23. 0, 0,
  24. false,
  25. gofpdf.ImageOptions{ImageType: "PNG", ReadDpi: true},
  26. 0,
  27. "",
  28. )
  29. return pdf.OutputFileAndClose(filename)
  30. }

有关更多信息和可用方法,请参见库的文档