Compare commits

...

13 commits
main ... pdf

Author SHA1 Message Date
jacs
49ceb4236d Merge branch 'pdf' of git.smarteching.com:jacs/cup into pdf 2024-12-22 10:39:16 -05:00
jacs
d840d3eba4 add sample xlsx 2024-12-22 10:37:58 -05:00
3c0a2ba991 Merge pull request 'develop' (#2) from develop into pdf
Reviewed-on: jacs/cup#2
2024-12-22 09:54:51 -05:00
7c033cba1d Merge pull request 'develop' (#1) from goffee/cup:develop into develop
Reviewed-on: jacs/cup#1
2024-12-22 09:50:19 -05:00
jacs
83f0db05da sample pdf with graph 2024-12-08 09:17:09 -05:00
ce78691f33 Merge pull request 'add gopher_read.png image, remove .env-dev from gitignore' (#10) from develop into main
Reviewed-on: #10
2024-12-08 08:55:21 -05:00
f22e940279 Merge pull request 'develop' (#9) from develop into main
Reviewed-on: #9
2024-12-06 05:02:05 -05:00
e85dea89b5 Merge pull request 'js destroy session' (#8) from develop into main
Reviewed-on: #8
2024-10-29 13:46:14 -04:00
7a44f2ede7 Merge pull request 'add prod mode' (#7) from develop into main
Reviewed-on: #7
2024-10-29 07:51:59 -04:00
b5f6b2f413 Merge pull request 'develop' (#6) from develop into main
Reviewed-on: #6
2024-10-28 13:21:41 -04:00
baeec24df2 Merge pull request 'develop' (#5) from develop into main
Reviewed-on: #5
2024-10-23 08:12:37 -04:00
0e2803df9e Merge pull request 'develop' (#4) from develop into main
Reviewed-on: #4
2024-10-16 00:00:20 -04:00
2f35c6e792 Merge pull request 'add theme system' (#1) from develop into main
Reviewed-on: #1
2024-09-27 10:49:30 -04:00
2 changed files with 204 additions and 0 deletions

202
controllers/pdfsample.go Normal file
View file

@ -0,0 +1,202 @@
// 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"
// 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
}

View file

@ -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)