This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwkhtmltoimage.go
193 lines (169 loc) · 4.62 KB
/
wkhtmltoimage.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Package wkhtmltoimage provides a simple wrapper around wkhtmltoimage (http://wkhtmltopdf.org/) binary.
package main
import (
"bytes"
"errors"
"image/jpeg"
"image/png"
"log"
"os/exec"
"strconv"
"strings"
)
// ImageOptions represent the options to generate the image.
type ImageOptions struct {
// BinaryPath the path to your wkhtmltoimage binary. REQUIRED
//
// Must be absolute path e.g /usr/local/bin/wkhtmltoimage
BinaryPath string
// Input is the content to turn into an image. REQUIRED
//
// Can be a url (http://example.com), a local file (/tmp/example.html), or html as a string (send "-" and set the HTML value)
Input string
// Format is the type of image to generate
//
// jpg, png, svg, bmp supported. Defaults to local wkhtmltoimage default
Format string
// Height is the height of the screen used to render in pixels.
//
// Default is calculated from page content. Default 0 (renders entire page top to bottom)
Height int
// Width is the width of the screen used to render in pixels.
//
// Note that this is used only as a guide line. Default 1024
Width int
// Quality determines the final image quality.
//
// Values supported between 1 and 100. Default is 94
Quality int
// Crop-x determines the final image crop from x
CropX int
// Crop-y determines the final image crop from y
CropY int
// Crop-w determines the final image crop width
CropW int
// Crop-h determines the final image crop height
CropH int
// HTML is a string of html to render into and image.
//
// Only needed to be set if Input is set to "-"
HTML string
// Output controls how to save or return the image.
//
// Leave nil to return a []byte of the image. Set to a path (/tmp/example.png) to save as a file.
Output string
}
// GenerateImage creates an image from an input.
// It returns the image ([]byte) and any error encountered.
func GenerateImage(options *ImageOptions) ([]byte, error) {
arr, err := buildParams(options)
if err != nil {
return []byte{}, err
}
if options.BinaryPath == "" {
return []byte{}, errors.New("BinaryPath not set")
}
cmd := exec.Command(options.BinaryPath, arr...)
if options.HTML != "" {
cmd.Stdin = strings.NewReader(options.HTML)
}
output, err := cmd.CombinedOutput()
if err != nil {
log.Println("can't generate file ", err)
return nil, err
}
if options.Output == "" && len(output) > 0 {
trimmed := cleanupOutput(output, options.Format)
return trimmed, err
} else {
return output, err
}
}
// buildParams takes the image options set by the user and turns them into command flags for wkhtmltoimage
// It returns an array of command flags.
func buildParams(options *ImageOptions) ([]string, error) {
a := []string{}
if options.Input == "" {
return []string{}, errors.New("Must provide input")
}
// silence extra wkhtmltoimage output
// might want to add --javascript-delay too?
a = append(a, "-q")
a = append(a, "--disable-plugins")
a = append(a, "--format")
if options.Format != "" {
a = append(a, options.Format)
} else {
a = append(a, "png")
}
if options.Height != 0 {
a = append(a, "--height")
a = append(a, strconv.Itoa(options.Height))
}
if options.Width != 0 {
a = append(a, "--width")
a = append(a, strconv.Itoa(options.Width))
}
if options.Quality != 0 {
a = append(a, "--quality")
a = append(a, strconv.Itoa(options.Quality))
}
if options.CropX != 0 {
a = append(a, "--crop-x")
a = append(a, strconv.Itoa(options.CropX))
}
if options.CropY != 0 {
a = append(a, "--crop-y")
a = append(a, strconv.Itoa(options.CropX))
}
if options.CropW != 0 {
a = append(a, "--crop-w")
a = append(a, strconv.Itoa(options.CropX))
}
if options.CropH != 0 {
a = append(a, "--crop-h")
a = append(a, strconv.Itoa(options.CropX))
}
// url and output come last
if options.Input != "-" {
// make sure we dont pass stdin if we aren't expecting it
options.HTML = ""
}
a = append(a, options.Input)
if options.Output == "" {
a = append(a, "-")
} else {
a = append(a, options.Output)
}
return a, nil
}
func cleanupOutput(img []byte, format string) []byte {
buf := new(bytes.Buffer)
switch {
case format == "png":
decoded, err := png.Decode(bytes.NewReader(img))
for err != nil {
img = img[1:]
if len(img) == 0 {
break
}
decoded, err = png.Decode(bytes.NewReader(img))
}
png.Encode(buf, decoded)
return buf.Bytes()
case format == "jpg":
decoded, err := jpeg.Decode(bytes.NewReader(img))
for err != nil {
img = img[1:]
if len(img) == 0 {
break
}
decoded, err = jpeg.Decode(bytes.NewReader(img))
}
jpeg.Encode(buf, decoded, nil)
return buf.Bytes()
// case format == "svg":
// return img
}
return img
}