forked from goffee/cup
139 lines
3.1 KiB
Go
139 lines
3.1 KiB
Go
// Copyright (c) 2024 Jose Cely <jose.cely@gmail.com>
|
|
// 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
|
|
}
|