From 83f0db05da9817e793edc75224c36e1998f58547 Mon Sep 17 00:00:00 2001 From: jacs Date: Sun, 8 Dec 2024 09:17:09 -0500 Subject: [PATCH] sample pdf with graph --- controllers/pdfsample.go | 139 +++++++++++++++++++++++++++++++++++++++ routes.go | 2 + 2 files changed, 141 insertions(+) create mode 100644 controllers/pdfsample.go diff --git a/controllers/pdfsample.go b/controllers/pdfsample.go new file mode 100644 index 0000000..e1eca71 --- /dev/null +++ b/controllers/pdfsample.go @@ -0,0 +1,139 @@ +// Copyright (c) 2024 Jose Cely +// Use of this source code is governed by MIT-style +// license that can be found in the LICENSE file. + +package controllers + +import ( + "bytes" + "fmt" + "io" + "time" + + "git.smarteching.com/goffee/core" + "git.smarteching.com/zeni/go-chart/v2" + "github.com/jung-kurt/gofpdf" +) + +// sample buffer back, pdf +func Themepdf(c *core.Context) *core.Response { + + var b bytes.Buffer + pw := io.Writer(&b) + //pr := io.Reader(&b) + + marginX := 10.0 + marginY := 20.0 + + pdf := gofpdf.New("P", "mm", "A4", "") + pdf.AddPage() + pdf.SetFont("Arial", "B", 16) + + pdf.ImageOptions("storage/public/img/gopher_read.png", marginY, marginX, 25, 25, false, gofpdf.ImageOptions{ImageType: "PNG", ReadDpi: true}, 0, "") + + pdf.SetXY(140, marginX) + pdf.SetFont("Arial", "B", 16) + + //_, lineHeight := pdf.GetFontSize() + + t := time.Now() + dateStr := t.Format("2006-01-02 15:04") + + pdf.Cell(0, 10, "Report XYZ") + pdf.SetXY(140, 20) + pdf.SetFont("Arial", "B", 12) + pdf.Cell(0, 10, dateStr) + + // Draw the table + + pdf.SetXY(marginX, 50) + + lineHt := 8.0 + + const colNumber = 2 + header := [colNumber]string{"No", "Description"} + colWidth := [colNumber]float64{20.0, 70} + + // Headers + pdf.SetFont("Arial", "B", 12) + + pdf.SetFillColor(200, 200, 200) + for colJ := 0; colJ < colNumber; colJ++ { + pdf.CellFormat(colWidth[colJ], lineHt, header[colJ], "1", 0, "CM", true, 0, "") + } + + pdf.Ln(-1) + pdf.SetFillColor(255, 255, 255) + + // Table data + pdf.SetFontStyle("") + + contents := getContents() + + for _, content := range contents { + pdf.CellFormat(colWidth[0], lineHt, content[0], "1", 0, "CM", true, 0, "") + pdf.CellFormat(colWidth[1], lineHt, content[1], "1", 0, "LM", true, 0, "") + pdf.Ln(-1) + } + + chartpng := getImageBuffer() + //r := bytes.NewReader(chartpng) + + //httpimg.Register(pdf, url, "") + //pdf.Image(url, 110, 50, 80, 80, false, "", 0, "") + //pdf.ImageOptions("storage/public/img/biplane.jpg", 110, 50, 80, 80, false, gofpdf.ImageOptions{ImageType: "JPEG", ReadDpi: true}, 0, "") + + pdf.RegisterImageOptionsReader("pcart.png", gofpdf.ImageOptions{ImageType: "PNG", ReadDpi: true}, chartpng) + pdf.ImageOptions("pcart.png", 110, 50, 80, 80, false, gofpdf.ImageOptions{ImageType: "JPEG", ReadDpi: true}, 0, "") + + pdf.Ln(lineHt) + pdf.Ln(lineHt) + + pdf.SetFont("Arial", "", 12) + _, lineHeight := pdf.GetFontSize() + + pdf.Cell(0, lineHeight, "At vero eos et accusamus et iusto odio dignissimos dntium voluptatum deleniti atqu.") + + // generate PDF + //pdf.OutputFileAndClose("hello.pdf") + + err := pdf.Output(pw) + if err != nil { + fmt.Println(err) + + } + + return c.Response.BufferPDF("elreport.pdf", b) + +} + +func getContents() [][]string { + return [][]string{ + {"node1", "Swamp"}, + {"lore1", "Sorin, A Planeswalker"}, + {"dia1", "Tassa"}, + {"ter3", "Skinrender"}, + {"bgt5", "Island"}, + {"weww", "Mountain"}, + {"asda", "Plain"}, + {"tetra", "Time Walk"}, + } +} + +func getImageBuffer() *bytes.Buffer { + pie := chart.PieChart{ + Width: 512, + Height: 512, + Values: []chart.Value{ + {Value: 8, Label: "Blue"}, + {Value: 5, Label: "Green"}, + {Value: 2, Label: "Gray"}, + {Value: 2, Label: "Orange"}, + }, + } + + buffer := bytes.NewBuffer([]byte{}) + pie.Render(chart.PNG, buffer) + + return buffer +} diff --git a/routes.go b/routes.go index 785c59e..d9c7fcc 100644 --- a/routes.go +++ b/routes.go @@ -27,6 +27,8 @@ func registerRoutes() { controller.Get("/themepanel", controllers.Themedemo) controller.Get("/themeelements", controllers.ThemeElements) + controller.Get("/themepdf", controllers.Themepdf) + // Uncomment the lines below to enable authentication controller.Post("/signup", controllers.Signup) controller.Post("/signin", controllers.Signin)