Skip to content

Commit

Permalink
Adjust save PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
mvrpl committed Aug 1, 2023
1 parent d1b7215 commit b8e17ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
36 changes: 17 additions & 19 deletions generate_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"encoding/json"
"fmt"
"math"
"os"
"path/filepath"
"strings"
"text/template"
"time"

wkhtmltopdf "github.com/SebastiaanKlippert/go-wkhtmltopdf"
"github.com/ConvertAPI/convertapi-go"
"github.com/ConvertAPI/convertapi-go/config"
"github.com/ConvertAPI/convertapi-go/param"
"github.com/dustin/go-humanize"
"github.com/syndtr/goleveldb/leveldb"
chart "github.com/wcharczuk/go-chart"
)

type Data struct {
Expand Down Expand Up @@ -165,15 +167,7 @@ func GenMedian() (int, int, int) {
}

func GenReport(montYer string) {
pdfg, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
panic(err)
}

pdfg.Dpi.Set(300)
pdfg.Orientation.Set(wkhtmltopdf.OrientationPortrait)

graph := chart.Chart{
/* graph := chart.Chart{
Series: []chart.Series{
chart.ContinuousSeries{
XValues: []float64{1.0, 2.0, 3.0, 4.0},
Expand All @@ -183,12 +177,14 @@ func GenReport(montYer string) {
}
buffer := bytes.NewBuffer([]byte{})
err = graph.Render(chart.PNG, buffer)
err := graph.Render(chart.PNG, buffer)
if err != nil {
panic(err)
}
// encodedText := base64.StdEncoding.EncodeToString(buffer.Bytes())
encodedText := base64.StdEncoding.EncodeToString(buffer.Bytes()) */

config.Default.Secret = os.Getenv("API_SECRET")

table, sourceConn := GenTable()

Expand All @@ -197,17 +193,19 @@ func GenReport(montYer string) {
html := fmt.Sprintf(`<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><html><center><h1>Relatório %s</h1></center><br>Origem: %s<br><br>%s<br><b>Média Ping: %dms | Média Download: %d Mbps | Média Upload: %d Mbps</b></html>`,
montYer, sourceConn, table, ping, down, upl)

pdfg.AddPage(wkhtmltopdf.NewPageReader(strings.NewReader(html)))

err = pdfg.Create()
htmlFile, err := os.Create("report.html")
if err != nil {
panic(err)
}

fileName := strings.ToLower(strings.Replace(montYer, "/", "", -1))

err = pdfg.WriteFile(fmt.Sprintf("report_%s.pdf", fileName))
_, err = htmlFile.WriteString(html)
if err != nil {
panic(err)
}

fileName := strings.ToLower(strings.Replace(montYer, "/", "", -1))

convertapi.ConvDef("html", "pdf",
param.NewPath("File", "report.html", nil),
).ToPath(fmt.Sprintf("report_%s.pdf", fileName))
}
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ module mvrpl.dev/speed-test
go 1.20

require (
github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.0
github.com/ConvertAPI/convertapi-go v0.0.0-20230704115642-e0ca20c26892
github.com/dustin/go-humanize v1.0.1
github.com/goodsign/monday v1.0.1
github.com/showwin/speedtest-go v1.6.3
github.com/syndtr/goleveldb v1.0.0
github.com/urfave/cli/v2 v2.25.7
github.com/wcharczuk/go-chart v2.0.1+incompatible
golang.org/x/text v0.11.0
)

require (
github.com/blend/go-sdk v1.20220411.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.7.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/image v0.9.0 // indirect
golang.org/x/net v0.6.0 // indirect
)

0 comments on commit b8e17ec

Please sign in to comment.