forked from josheyr/svg2png
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml2png_test.go
51 lines (42 loc) · 1.46 KB
/
html2png_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package html2png
import (
"context"
"github.com/playwright-community/playwright-go"
"os"
"testing"
"time"
)
const (
testHtml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" width=\"500\" height=\"500\" viewBox=\"0 0 500 500\" xml:space=\"preserve\">\n<desc>Created with Fabric.js 5.2.4</desc>\n<defs>\n</defs>\n<g transform=\"matrix(6.54 0 0 6.54 253.46 242.97)\" >\n<circle style=\"stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,85,85); fill-rule: nonzero; opacity: 1;\" cx=\"0\" cy=\"0\" r=\"30\" />\n</g>\n</svg>"
)
func TestHtmlToPng(t *testing.T) {
err := playwright.Install(&playwright.RunOptions{
Browsers: []string{"chromium"},
Verbose: false,
})
if err != nil {
t.Error(err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
pngitem, err := HtmlToPng(ctx, testHtml, 500, 500)
if err != nil {
t.Error(err)
}
// show png
file, err := os.Create("test.png")
if err != nil {
t.Error(err)
}
_, err = file.Write(pngitem)
if err != nil {
t.Error(err)
}
err = file.Close()
if err != nil {
t.Error(err)
}
// print file path
t.Log("file path: " + file.Name())
}