1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package main
- import (
- "bytes"
- "fmt"
- "git.aionnect.com/aionnect/go-common/utils"
- "github.com/cbroglie/mustache"
- "io/ioutil"
- "os"
- "os/exec"
- "strconv"
- )
- const tplStr = `<html>
- <body style="border: 0; padding: 0; margin: 0">
- <div style="width: 768px; height: 1024px; text-align: center; background-color: bisque">
- <h3 style="padding-top:20px">{{title}}</h3>
- <p>{{description}}</p>
- <img src="{{url}}" width="430" height="430" />
- </div>
- </body>
- </html>`
- const (
- W = 768
- H = 1024
- )
- func main() {
- var buf bytes.Buffer
- tpl, _ := mustache.ParseString(tplStr)
- _ = tpl.FRender(&buf, map[string]string{
- "title": "Hello world",
- "description": "A test for convert html to jpg",
- "url": "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFe8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyUFlBM1FKcDlldWgxMDAwMDAwNzAAAgTryl1eAwQAAAAA",
- })
- tempId := utils.NextId()
- htmlFileName := fmt.Sprintf("temp_%d.html", tempId)
- imgFileName := fmt.Sprintf("temp_%d.jpg", tempId)
- _ = ioutil.WriteFile(htmlFileName, buf.Bytes(), os.ModeAppend)
- cmd := exec.Command(`wkhtmltoimage`, `--height`, strconv.Itoa(H), `--width`, strconv.Itoa(W), htmlFileName, imgFileName)
- _ = cmd.Run()
- // output, err := cmd.CombinedOutput()
- }
|