diff --git a/controllers/pdfsample.go b/controllers/pdfsample.go new file mode 100644 index 0000000..29a449e --- /dev/null +++ b/controllers/pdfsample.go @@ -0,0 +1,202 @@ +// 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" + // for graphs + "git.smarteching.com/zeni/go-chart/v2" + // for PDF + "github.com/jung-kurt/gofpdf" + // for xslx + "github.com/xuri/excelize/v2" +) + +// 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.BufferFile("elreport.pdf", "application/pdf", b) + +} + +func Themexslx(c *core.Context) *core.Response { + + var b bytes.Buffer + + f := excelize.NewFile() + + defer func() { + if err := f.Close(); err != nil { + fmt.Println(err) + } + }() + + for idx, row := range [][]interface{}{ + {nil, "Cerveza", "Vodka", "Ron"}, {"Small", 2, 3, 3}, + {"Normal", 5, 2, 4}, {"Large", 6, 7, 8}, + } { + cell, err := excelize.CoordinatesToCellName(1, idx+1) + if err != nil { + fmt.Println(err) + } + f.SetSheetRow("Sheet1", cell, &row) + } + + if err := f.AddChart("Sheet1", "E1", &excelize.Chart{ + Type: excelize.Pie, + Series: []excelize.ChartSeries{ + { + Name: "Totales", + Categories: "Sheet1!$B$1:$D$1", + Values: "Sheet1!$B$2:$D$2", + }, + }, + Format: excelize.GraphicOptions{ + OffsetX: 15, + OffsetY: 10, + }, + Title: []excelize.RichTextRun{ + { + Text: "Fruit Pie Chart", + }, + }, + PlotArea: excelize.ChartPlotArea{ + ShowPercent: true, + }, + }); err != nil { + fmt.Println(err) + } + + buf, err := f.WriteToBuffer() + if err != nil { + fmt.Fprint(buf, err.Error()) + } + + // Get byte array + b.ReadFrom(buf) + + return c.Response.BufferFile("elreport.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 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)